]> granicus.if.org Git - postgis/commitdiff
Avoid undefined behaviour in next_float functions (Raúl Marín)
authorRaúl Marín Rodríguez <rmrodriguez@carto.com>
Wed, 21 Nov 2018 11:46:43 +0000 (11:46 +0000)
committerRaúl Marín Rodríguez <rmrodriguez@carto.com>
Wed, 21 Nov 2018 11:46:43 +0000 (11:46 +0000)
Closes #4247
Closes https://github.com/postgis/postgis/pull/339

git-svn-id: http://svn.osgeo.org/postgis/trunk@17038 b70326c6-7e19-0410-871a-916f4a2858ee

NEWS
liblwgeom/cunit/cu_libgeom.c
liblwgeom/lwgeom_api.c

diff --git a/NEWS b/NEWS
index ac3ba0c709b8a75942826d053ad353590e84468a..9111d6d14192667d5b0326c459371b316a381b1c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -39,6 +39,7 @@ PostGIS 3.0.0
   - #4326, Allocate enough memory in gidx_to_string (Raúl Marín)
   - #4190, Avoid undefined behaviour in gserialized_estimate (Raúl Marín)
   - #4233, Fix undefined behaviour in gserialized_spgist_picksplit_nd (Raúl Marín)
+  - #4247, Avoid undefined behaviour in next_float functions (Raúl Marín)
 
 PostGIS 2.5.0
 2018/09/23
index 55625d2eeaec761c1ef5b5f54e383d5304ac953a..43c2c9907e6814a394132d97df30a5b1cbd45a46 100644 (file)
@@ -688,6 +688,26 @@ static void test_f2d(void)
        f = next_float_up(d);
        d = next_float_up(f);
        CU_ASSERT_DOUBLE_EQUAL(f,d, 0.0000001);
+
+       d = DBL_MAX;
+       f = next_float_up(d);
+       d = next_float_up(f);
+       CU_ASSERT_DOUBLE_EQUAL(f, d, 0.0000001);
+
+       d = DBL_MAX;
+       f = next_float_down(d);
+       d = next_float_down(f);
+       CU_ASSERT_DOUBLE_EQUAL(f, d, 0.0000001);
+
+       d = -DBL_MAX;
+       f = next_float_up(d);
+       d = next_float_up(f);
+       CU_ASSERT_DOUBLE_EQUAL(f, d, 0.0000001);
+
+       d = -DBL_MAX;
+       f = next_float_down(d);
+       d = next_float_down(f);
+       CU_ASSERT_DOUBLE_EQUAL(f, d, 0.0000001);
 }
 
 /*
index 27515ee5d95a2038714b838f176b17c49d989e40..ac0dce80aa84bdeb0c3ea6799be27107c9995479 100644 (file)
@@ -50,7 +50,12 @@ lwgeom_version()
 inline float
 next_float_down(double d)
 {
-       float result  = d;
+       float result;
+       if (d > (double)FLT_MAX)
+               return FLT_MAX;
+       if (d <= (double)-FLT_MAX)
+               return -FLT_MAX;
+       result = d;
 
        if ( ((double)result) <=d )
                return result;
@@ -66,7 +71,12 @@ next_float_down(double d)
 inline float
 next_float_up(double d)
 {
-       float result  = d;
+       float result;
+       if (d >= (double)FLT_MAX)
+               return FLT_MAX;
+       if (d < (double)-FLT_MAX)
+               return -FLT_MAX;
+       result = d;
 
        if ( ((double)result) >=d )
                return result;