From 2182c81858087abe1934e112f0e5c7d96b8138b4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ra=C3=BAl=20Mar=C3=ADn=20Rodr=C3=ADguez?= Date: Tue, 2 Oct 2018 09:38:48 +0000 Subject: [PATCH] Fix undefined behaviour in SADFWrite Also addresses several GCC warnings. Closes #4189 Closes https://github.com/postgis/postgis/pull/310 git-svn-id: http://svn.osgeo.org/postgis/trunk@16863 b70326c6-7e19-0410-871a-916f4a2858ee --- NEWS | 1 + liblwgeom/lwutil.c | 8 ++++---- loader/pgsql2shp-core.c | 2 +- loader/safileio.c | 5 +++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 7423db1f3..3c55f4541 100644 --- a/NEWS +++ b/NEWS @@ -21,6 +21,7 @@ PostGIS 3.0.0 - #4181, St_AsMVTGeom: Avoid type changes due to validation (Raúl Marín) - #4183, St_AsMVTGeom: Drop invalid geometries after simplification (Raúl Marín) - #4188, Avoid division by zero in kmeans (Raúl Marín) + - #4189, Fix undefined behaviour in SADFWrite (Raúl Marín) PostGIS 2.5.0 2018/09/23 diff --git a/liblwgeom/lwutil.c b/liblwgeom/lwutil.c index b9a523c66..52f172007 100644 --- a/liblwgeom/lwutil.c +++ b/liblwgeom/lwutil.c @@ -280,13 +280,13 @@ char *lwmessage_truncate(char *str, int startpos, int endpos, int maxlength, int { /* Add "..." prefix */ outstart = str + endpos + 1 - maxlength + 3; - strncat(output, "...", 3); + strncat(output, "...", 4); strncat(output, outstart, maxlength - 3); } else { /* maxlength is too small; just output "..." */ - strncat(output, "...", 3); + strncat(output, "...", 4); } } } @@ -307,12 +307,12 @@ char *lwmessage_truncate(char *str, int startpos, int endpos, int maxlength, int /* Add "..." suffix */ outstart = str + startpos; strncat(output, outstart, maxlength - 3); - strncat(output, "...", 3); + strncat(output, "...", 4); } else { /* maxlength is too small; just output "..." */ - strncat(output, "...", 3); + strncat(output, "...", 4); } } } diff --git a/loader/pgsql2shp-core.c b/loader/pgsql2shp-core.c index ffbc613c6..aa9c26b5e 100644 --- a/loader/pgsql2shp-core.c +++ b/loader/pgsql2shp-core.c @@ -1549,7 +1549,7 @@ ShpDumperOpenTable(SHPDUMPERSTATE *state) { if (!strncasecmp(dbffieldname, state->dbffieldnames[j], 10)) { - sprintf(dbffieldname, "%.7s_%.2d", ptr, tmpint % 100); + sprintf(dbffieldname, "%.7s_%.2d", ptr, abs(tmpint) % 100); tmpint++; continue; } diff --git a/loader/safileio.c b/loader/safileio.c index 439ebb141..ee5a98afb 100644 --- a/loader/safileio.c +++ b/loader/safileio.c @@ -115,8 +115,9 @@ SAOffset SADFRead( void *p, SAOffset size, SAOffset nmemb, SAFile file ) SAOffset SADFWrite( void *p, SAOffset size, SAOffset nmemb, SAFile file ) { - return (SAOffset) fwrite( p, (size_t) size, (size_t) nmemb, - (FILE *) file ); + if (!nmemb || !p) return 0; + return (SAOffset) fwrite( p, (size_t) size, (size_t) nmemb, + (FILE *) file ); } /************************************************************************/ -- 2.40.0