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

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


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

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

References #4247



Modified: branches/2.3/NEWS
===================================================================
--- branches/2.3/NEWS	2018-11-19 16:21:03 UTC (rev 17034)
+++ branches/2.3/NEWS	2018-11-21 11:43:09 UTC (rev 17035)
@@ -16,6 +16,7 @@
   - #3457, Fix raster envelope shortcut in ST_Clip (Sai-bot)
   - #4223, Fix parallel/near rectangle case in geography (Paul Ramsey)
   - #4326, Allocate enough memory in gidx_to_string (Raúl Marín)
+  - #4247, Avoid undefined behaviour in next_float functions (Raúl Marín)
 
 PostGIS 2.3.7
 2018/04/06

Modified: branches/2.3/liblwgeom/cunit/cu_libgeom.c
===================================================================
--- branches/2.3/liblwgeom/cunit/cu_libgeom.c	2018-11-19 16:21:03 UTC (rev 17034)
+++ branches/2.3/liblwgeom/cunit/cu_libgeom.c	2018-11-21 11:43:09 UTC (rev 17035)
@@ -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.3/liblwgeom/lwgeom_api.c
===================================================================
--- branches/2.3/liblwgeom/lwgeom_api.c	2018-11-19 16:21:03 UTC (rev 17034)
+++ branches/2.3/liblwgeom/lwgeom_api.c	2018-11-21 11:43:09 UTC (rev 17035)
@@ -155,7 +155,12 @@
 
 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;
@@ -171,7 +176,12 @@
 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