]> granicus.if.org Git - postgis/commitdiff
Multiple fixes for undefined behaviour in implicit conversions
authorRaúl Marín Rodríguez <rmrodriguez@carto.com>
Wed, 24 Apr 2019 11:07:56 +0000 (11:07 +0000)
committerRaúl Marín Rodríguez <rmrodriguez@carto.com>
Wed, 24 Apr 2019 11:07:56 +0000 (11:07 +0000)
shp2pgsql-core.c:839:22: runtime error: implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'DBFFieldType' changed the value to 4294967295 (32-bit, unsigned)

 runtime error: implicit conversion from type 'int32' (aka 'int') of value -1 (32-bit, signed) to type 'uint32' (aka 'unsigned int') changed the value to 4294967295 (32-bit, unsigned)
 UndefinedBehaviorSanitizer: undefined-behavior lwgeom_functions_basic.c:2237:10 in

 runtime error: implicit conversion from type 'unsigned int' of value 4294967295 (32-bit, unsigned) to type 'int' changed the value to -1 (32-bit, signed)
 UndefinedBehaviorSanitizer: undefined-behavior ptarray.c:333:13 in

 runtime error: implicit conversion from type 'unsigned int' of value 4294967295 (32-bit, unsigned) to type 'int' changed the value to -1 (32-bit, signed)
 UndefinedBehaviorSanitizer: undefined-behavior ptarray.c:333:13 in

References #4383

git-svn-id: http://svn.osgeo.org/postgis/trunk@17414 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/ptarray.c
loader/shp2pgsql-core.c
postgis/lwgeom_functions_basic.c

index 594d2d2ebfc913ff39594eefc85a5d2d89471256..2140caba7aee9c118777585a872dbb6aa9864472 100644 (file)
@@ -329,9 +329,11 @@ void ptarray_free(POINTARRAY *pa)
 void
 ptarray_reverse_in_place(POINTARRAY *pa)
 {
-       int i;
-       int last = pa->npoints-1;
-       int mid = pa->npoints/2;
+       if (!pa->npoints)
+               return;
+       uint32_t i;
+       uint32_t last = pa->npoints - 1;
+       uint32_t mid = pa->npoints / 2;
 
        double *d = (double*)(pa->serialized_pointlist);
        int j;
index a2c869774fdabb31a0bc03db35f628bda6799200..b11f0eb24c8764c8d33a9443e39fbfda390c1d91 100644 (file)
@@ -836,7 +836,7 @@ ShpLoaderOpenShape(SHPLOADERSTATE *state)
        int field_precision, field_width;
        char name[MAXFIELDNAMELEN];
        char name2[MAXFIELDNAMELEN];
-       DBFFieldType type = -1;
+       DBFFieldType type = FTInvalid;
        char *utf8str;
 
        /* If we are reading the entire shapefile, open it */
index cd7281e7c3ee92e1870f70c9ea2d726f44e75387..3a0866ba8e42b424b38ff23de7ef4108820ce7c4 100644 (file)
@@ -2229,7 +2229,7 @@ Datum LWGEOM_removepoint(PG_FUNCTION_ARGS)
 {
        GSERIALIZED *pglwg1, *result;
        LWLINE *line, *outline;
-       uint32 which;
+       int32 which;
 
        POSTGIS_DEBUG(2, "LWGEOM_removepoint called.");
 
@@ -2244,9 +2244,9 @@ Datum LWGEOM_removepoint(PG_FUNCTION_ARGS)
 
        line = lwgeom_as_lwline(lwgeom_from_gserialized(pglwg1));
 
-       if (which > line->points->npoints - 1)
+       if (which < 0 || (uint32_t)which > line->points->npoints - 1)
        {
-               elog(ERROR, "Point index out of range (%d..%d)", 0, line->points->npoints - 1);
+               elog(ERROR, "Point index out of range (%u..%u)", 0, line->points->npoints - 1);
                PG_RETURN_NULL();
        }
 
@@ -2256,7 +2256,7 @@ Datum LWGEOM_removepoint(PG_FUNCTION_ARGS)
                PG_RETURN_NULL();
        }
 
-       outline = lwline_removepoint(line, which);
+       outline = lwline_removepoint(line, (uint32_t)which);
        /* Release memory */
        lwline_free(line);
 
@@ -2275,7 +2275,7 @@ Datum LWGEOM_setpoint_linestring(PG_FUNCTION_ARGS)
        LWLINE *line;
        LWPOINT *lwpoint;
        POINT4D newpoint;
-       int32 which;
+       int64_t which;
 
        POSTGIS_DEBUG(2, "LWGEOM_setpoint_linestring called.");
 
@@ -2307,11 +2307,11 @@ Datum LWGEOM_setpoint_linestring(PG_FUNCTION_ARGS)
        if (which < 0)
        {
                /* Use backward indexing for negative values */
-               which = which + line->points->npoints;
+               which += (int64_t)line->points->npoints;
        }
-       if ((uint32_t)which + 1 > line->points->npoints)
+       if ((uint32_t)which > line->points->npoints - 1)
        {
-               elog(ERROR, "abs(Point index) out of range (-)(%d..%d)", 0, line->points->npoints - 1);
+               elog(ERROR, "abs(Point index) out of range (-)(%u..%u)", 0, line->points->npoints - 1);
                PG_RETURN_NULL();
        }