[postgis-tickets] r17744 - Replace aliasing with memcpy

Paul Ramsey pramsey at cleverelephant.ca
Tue Aug 20 11:33:34 PDT 2019


Author: pramsey
Date: 2019-08-20 11:33:34 -0700 (Tue, 20 Aug 2019)
New Revision: 17744

Modified:
   trunk/liblwgeom/lwgeom.c
Log:
Replace aliasing with memcpy
References #4319


Modified: trunk/liblwgeom/lwgeom.c
===================================================================
--- trunk/liblwgeom/lwgeom.c	2019-08-20 18:33:00 UTC (rev 17743)
+++ trunk/liblwgeom/lwgeom.c	2019-08-20 18:33:34 UTC (rev 17744)
@@ -2444,28 +2444,21 @@
 	return bits_needed;
 }
 
-static inline
-double mask_double(double d, uint64_t mask)
-{
-	uint64_t* double_bits = (uint64_t*) (&d);
-
-	(*double_bits) &= mask;
-
-	return *((double*) double_bits);
-}
-
 static double trim_preserve_decimal_digits(double d, int32_t decimal_digits)
 {
-	if (d==0)
+	if (d == 0)
 		return 0;
 
 	int digits_left_of_decimal = (int) (1 + log10(fabs(d)));
 	uint8_t bits_needed = bits_for_precision(decimal_digits + digits_left_of_decimal);
-
-
 	uint64_t mask = 0xffffffffffffffff << (52 - bits_needed);
+	uint64_t dint = 0;
+	size_t dsz = sizeof(d) < sizeof(dint) ? sizeof(d) : sizeof(dint);
 
-	return mask_double(d, mask);
+	memcpy(&dint, &d, dsz);
+	dint &= mask;
+	memcpy(&d, &dint, dsz);
+	return d;
 }
 
 void lwgeom_trim_bits_in_place(LWGEOM* geom, int32_t prec_x, int32_t prec_y, int32_t prec_z, int32_t prec_m)



More information about the postgis-tickets mailing list