From 2e1bd0c7a7b68060c9d056f19bb174da48c56711 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ra=C3=BAl=20Mar=C3=ADn=20Rodr=C3=ADguez?= Date: Wed, 24 Apr 2019 11:07:56 +0000 Subject: [PATCH] Multiple fixes for undefined behaviour in implicit conversions 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 | 8 +++++--- loader/shp2pgsql-core.c | 2 +- postgis/lwgeom_functions_basic.c | 16 ++++++++-------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/liblwgeom/ptarray.c b/liblwgeom/ptarray.c index 594d2d2eb..2140caba7 100644 --- a/liblwgeom/ptarray.c +++ b/liblwgeom/ptarray.c @@ -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; diff --git a/loader/shp2pgsql-core.c b/loader/shp2pgsql-core.c index a2c869774..b11f0eb24 100644 --- a/loader/shp2pgsql-core.c +++ b/loader/shp2pgsql-core.c @@ -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 */ diff --git a/postgis/lwgeom_functions_basic.c b/postgis/lwgeom_functions_basic.c index cd7281e7c..3a0866ba8 100644 --- a/postgis/lwgeom_functions_basic.c +++ b/postgis/lwgeom_functions_basic.c @@ -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(); } -- 2.40.0