r17036 - Avoid undefined behaviour in next_float functions (Raúl Marín)

Raul raul at rmr.ninja
Wed Nov 21 03:43:57 PST 2018


Author: algunenano
Date: 2018-11-21 03:43:57 -0800 (Wed, 21 Nov 2018)
New Revision: 17036

Modified:
   branches/2.4/NEWS
   branches/2.4/liblwgeom/cunit/cu_libgeom.c
   branches/2.4/liblwgeom/lwgeom_api.c
Log:
Avoid undefined behaviour in next_float functions (Raúl Marín)

References #4247



Modified: branches/2.4/NEWS
===================================================================
--- branches/2.4/NEWS	2018-11-21 11:43:09 UTC (rev 17035)
+++ branches/2.4/NEWS	2018-11-21 11:43:57 UTC (rev 17036)
@@ -17,7 +17,8 @@
   - #4136, Proper repeated point removal on small polygons (Paul Ramsey)
   - #4223, ST_DistanceTree error for near parallel boxes (Paul Ramsey)
   - #4326, Allocate enough memory in gidx_to_string (Raúl Marín)
-  - #4190, Avoid undefined behaviour in gserialized_estimate
+  - #4190, Avoid undefined behaviour in gserialized_estimate (Raúl Marín)
+  - #4247, Avoid undefined behaviour in next_float functions (Raúl Marín)
 
 
 PostGIS 2.4.5

Modified: branches/2.4/liblwgeom/cunit/cu_libgeom.c
===================================================================
--- branches/2.4/liblwgeom/cunit/cu_libgeom.c	2018-11-21 11:43:09 UTC (rev 17035)
+++ branches/2.4/liblwgeom/cunit/cu_libgeom.c	2018-11-21 11:43:57 UTC (rev 17036)
@@ -688,6 +688,26 @@
 	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);
 }
 
 /*

Modified: branches/2.4/liblwgeom/lwgeom_api.c
===================================================================
--- branches/2.4/liblwgeom/lwgeom_api.c	2018-11-21 11:43:09 UTC (rev 17035)
+++ branches/2.4/liblwgeom/lwgeom_api.c	2018-11-21 11:43:57 UTC (rev 17036)
@@ -50,7 +50,12 @@
 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 @@
 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;



More information about the postgis-tickets mailing list