[postgis-tickets] r15350 - fix variable length arrays

Sandro Santilli strk at kbt.io
Wed Apr 12 06:33:53 PDT 2017


Author: strk
Date: 2017-04-12 06:33:53 -0700 (Wed, 12 Apr 2017)
New Revision: 15350

Modified:
   trunk/liblwgeom/lwgeom_geos.c
   trunk/liblwgeom/lwprint.c
   trunk/postgis/lwgeom_ogc.c
Log:
fix variable length arrays

Modified: trunk/liblwgeom/lwgeom_geos.c
===================================================================
--- trunk/liblwgeom/lwgeom_geos.c	2017-04-08 12:25:39 UTC (rev 15349)
+++ trunk/liblwgeom/lwgeom_geos.c	2017-04-12 13:33:53 UTC (rev 15350)
@@ -1649,24 +1649,7 @@
 }
 
 
-static void shuffle(void *array, size_t n, size_t size) {
-    char tmp[size];
-    char *arr = array;
-    size_t stride = size;
 
-    if (n > 1) {
-        size_t i;
-        for (i = 0; i < n - 1; ++i) {
-            size_t rnd = (size_t) rand();
-            size_t j = i + rnd / (RAND_MAX / (n - i) + 1);
-
-            memcpy(tmp, arr + j * stride, size);
-            memcpy(arr + j * stride, arr + i * stride, size);
-            memcpy(arr + i * stride, tmp, size);
-        }
-    }
-}
-
 LWMPOINT*
 lwpoly_to_points(const LWPOLY *lwpoly, int npoints)
 {
@@ -1675,7 +1658,7 @@
 	const LWGEOM *lwgeom = (LWGEOM*)lwpoly;
 	int sample_npoints, sample_sqrt, sample_width, sample_height;
 	double sample_cell_size;
-	int i, j;
+	int i, j, n;
 	int iterations = 0;
 	int npoints_generated = 0;
 	int npoints_tested = 0;
@@ -1687,7 +1670,11 @@
 	int srid = lwgeom_get_srid(lwgeom);
 	int done = 0;
 	int *cells;
+	const size_t size = 2*sizeof(int);
+	char tmp[2*sizeof(int)];
+	const size_t stride = 2*sizeof(int);
 
+
 	if (lwgeom_get_type(lwgeom) != POLYGONTYPE)
 	{
 		lwerror("%s: only polygons supported", __func__);
@@ -1773,8 +1760,23 @@
 			cells[2*(i*sample_height+j)+1] = j;
 		}
 	}
-	shuffle(cells, sample_height*sample_width, 2*sizeof(int));
 
+	/* shuffle */
+	{
+		n = sample_height*sample_width;
+		if (n > 1) {
+			for (i = 0; i < n - 1; ++i) {
+				size_t rnd = (size_t) rand();
+				size_t j = i + rnd / (RAND_MAX / (n - i) + 1);
+
+				memcpy(tmp, (char *)cells + j * stride, size);
+				memcpy((char *)cells + j * stride, (char *)cells + i * stride, size);
+				memcpy((char *)cells + i * stride, tmp, size);
+			}
+		}
+	}
+
+
 	/* Start testing points */
 	while (npoints_generated < npoints)
 	{

Modified: trunk/liblwgeom/lwprint.c
===================================================================
--- trunk/liblwgeom/lwprint.c	2017-04-08 12:25:39 UTC (rev 15349)
+++ trunk/liblwgeom/lwprint.c	2017-04-12 13:33:53 UTC (rev 15350)
@@ -76,8 +76,8 @@
 static char * lwdouble_to_dms(double val, const char *pos_dir_symbol, const char *neg_dir_symbol, const char * format)
 {
 	/* 3 numbers, 1 sign or compass dir, and 5 possible strings (degree signs, spaces, misc text, etc) between or around them.*/
-	static int NUM_PIECES = 9;
-	static int WORK_SIZE = 1024;
+#	define NUM_PIECES 9
+#	define WORK_SIZE 1024
 	char pieces[NUM_PIECES][WORK_SIZE];
 	int current_piece = 0;
 	int is_negative = 0;

Modified: trunk/postgis/lwgeom_ogc.c
===================================================================
--- trunk/postgis/lwgeom_ogc.c	2017-04-08 12:25:39 UTC (rev 15349)
+++ trunk/postgis/lwgeom_ogc.c	2017-04-12 13:33:53 UTC (rev 15350)
@@ -177,7 +177,7 @@
 {
 	GSERIALIZED *gser;
 	text *type_text;
-	static int type_str_len = 31;
+#	define type_str_len 31
 	char type_str[type_str_len + 1];
 
 	/* Read just the header from the toasted tuple */



More information about the postgis-tickets mailing list