From a540fa1453da66be455a2c23da18c6afd8cd681b Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Wed, 12 Apr 2017 13:33:53 +0000 Subject: [PATCH] fix variable length arrays git-svn-id: http://svn.osgeo.org/postgis/trunk@15350 b70326c6-7e19-0410-871a-916f4a2858ee --- liblwgeom/lwgeom_geos.c | 40 +++++++++++++++++++++------------------- liblwgeom/lwprint.c | 4 ++-- postgis/lwgeom_ogc.c | 2 +- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/liblwgeom/lwgeom_geos.c b/liblwgeom/lwgeom_geos.c index 64659dec0..558b6d7b6 100644 --- a/liblwgeom/lwgeom_geos.c +++ b/liblwgeom/lwgeom_geos.c @@ -1649,23 +1649,6 @@ lwgeom_offsetcurve(const LWLINE *lwline, double size, int quadsegs, int joinStyl } -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 @@ lwpoly_to_points(const LWPOLY *lwpoly, int npoints) 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,6 +1670,10 @@ lwpoly_to_points(const LWPOLY *lwpoly, int npoints) 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) { @@ -1773,7 +1760,22 @@ lwpoly_to_points(const LWPOLY *lwpoly, int npoints) 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) diff --git a/liblwgeom/lwprint.c b/liblwgeom/lwprint.c index 6557ec02a..649285e56 100644 --- a/liblwgeom/lwprint.c +++ b/liblwgeom/lwprint.c @@ -76,8 +76,8 @@ static void lwprint_normalize_latlon(double *lat, double *lon) 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; diff --git a/postgis/lwgeom_ogc.c b/postgis/lwgeom_ogc.c index 3db1744a1..5d909c5e9 100644 --- a/postgis/lwgeom_ogc.c +++ b/postgis/lwgeom_ogc.c @@ -177,7 +177,7 @@ Datum geometry_geometrytype(PG_FUNCTION_ARGS) { 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 */ -- 2.50.1