[postgis-tickets] r17670 - ST_GeomFromGeoJSON crash on empty rings

Darafei komzpa at gmail.com
Sun Aug 4 12:39:32 PDT 2019


Author: komzpa
Date: 2019-08-04 00:39:32 -0700 (Sun, 04 Aug 2019)
New Revision: 17670

Modified:
   branches/2.4/NEWS
   branches/2.4/liblwgeom/lwin_geojson.c
   branches/2.4/regress/in_geojson.sql
   branches/2.4/regress/in_geojson_expected
Log:
ST_GeomFromGeoJSON crash on empty rings

Closes #4470


Modified: branches/2.4/NEWS
===================================================================
--- branches/2.4/NEWS	2019-08-04 07:36:50 UTC (rev 17669)
+++ branches/2.4/NEWS	2019-08-04 07:39:32 UTC (rev 17670)
@@ -15,6 +15,7 @@
   - #4419, Use protobuf version to enable/disable mvt/geobuf (Paul Ramsey)
   - #4437, Handle POINT EMPTY in shape loader/dumper (Paul Ramsey)
   - #4461, ST_AsTWKB doesn't always remove duplicate points (Nik Aven)
+  - #4470, ST_GeomFromGeoJSON crash on empty rings (Darafei Praliaskouski)
 
 
 PostGIS 2.4.7

Modified: branches/2.4/liblwgeom/lwin_geojson.c
===================================================================
--- branches/2.4/liblwgeom/lwin_geojson.c	2019-08-04 07:36:50 UTC (rev 17669)
+++ branches/2.4/liblwgeom/lwin_geojson.c	2019-08-04 07:39:32 UTC (rev 17670)
@@ -52,7 +52,7 @@
 
 #include <string.h>
 
-static void geojson_lwerror(char *msg, int error_code)
+static void geojson_lwerror(char *msg, __attribute__((__unused__)) int error_code)
 {
 	LWDEBUGF(3, "lwgeom_from_geojson ERROR %i", error_code);
 	lwerror("%s", msg);
@@ -262,9 +262,6 @@
 		}
 		nPoints = json_object_array_length(points);
 
-		/* Skip empty rings */
-		if ( nPoints == 0 ) continue;
-
 		if ( ! ppa )
 			ppa = (POINTARRAY**)lwalloc(sizeof(POINTARRAY*) * nRings);
 
@@ -273,7 +270,17 @@
 		{
 			json_object* coords = NULL;
 			coords = json_object_array_get_idx( points, j );
-			parse_geojson_coord(coords, hasz, ppa[i]);
+			if (LW_FAILURE == parse_geojson_coord(coords, hasz, ppa[i]))
+			{
+				int k;
+				for (k = 0; k <= i; k++)
+				{
+					ptarray_free(ppa[k]);
+				}
+				lwfree(ppa);
+				geojson_lwerror("The 'coordinates' in GeoJSON polygon are not sufficiently nested", 4);
+				return NULL;
+			}
 		}
 	}
 

Modified: branches/2.4/regress/in_geojson.sql
===================================================================
--- branches/2.4/regress/in_geojson.sql	2019-08-04 07:36:50 UTC (rev 17669)
+++ branches/2.4/regress/in_geojson.sql	2019-08-04 07:39:32 UTC (rev 17670)
@@ -5,7 +5,8 @@
 select 'geomfromgeojson_04',st_astext(st_geomfromgeojson(st_asgeojson('LINESTRING(0 0,1 1)')));
 select 'geomfromgeojson_05',st_astext(st_geomfromgeojson(st_asgeojson('POLYGON((0 0,1 1,1 0,0 0))')));
 select 'geomfromgeojson_06',st_astext(st_geomfromgeojson(st_asgeojson('MULTIPOLYGON(((0 0,1 1,1 0,0 0)))')));
-
+select 'geomfromgeojson_07',st_astext(st_geomfromgeojson(st_asgeojson('MULTIPOLYGON(((0 0,1 1,1 0,0 0)))')::json));
+select 'geomfromgeojson_08',st_astext(st_geomfromgeojson(st_asgeojson('MULTIPOLYGON(((0 0,1 1,1 0,0 0)))')::jsonb));
 -- #1434
 select '#1434: Next two errors';
 select '#1434.1',ST_GeomFromGeoJSON('{ "type": "Point", "crashme": [100.0, 0.0] }');
@@ -48,3 +49,10 @@
 
 -- #3583
 SELECT '#3583', ST_AsText(ST_GeomFromGeoJSON('{"type":"MultiPolygon", "coordinates":[[[139.10030364990232,35.16777444430609],5842.4224490305424]]}'));
+
+-- #4164
+SELECT ST_AsText(ST_GeomFromGeoJSON('{"type": "Polygon", "coordinates": [[0,0],[0,5],[5, 5],[5,0],[0,0]]}'));
+SELECT '#4470.a', ST_AsText(ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[0,0]],[]]}'));
+SELECT '#4470.b', ST_AsText(ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[],[0,0]]}'));
+SELECT '#4470.c', ST_AsText(ST_GeomFromGeoJSON('{"type":"MultiPolygon","coordinates":[[[[0,0]],[]]]}'));
+SELECT '#4470.d', ST_AsText(ST_GeomFromGeoJSON('{"type":"MultiPolygon","coordinates":[[[],[0,0]]]}'));

Modified: branches/2.4/regress/in_geojson_expected
===================================================================
--- branches/2.4/regress/in_geojson_expected	2019-08-04 07:36:50 UTC (rev 17669)
+++ branches/2.4/regress/in_geojson_expected	2019-08-04 07:39:32 UTC (rev 17670)
@@ -4,6 +4,8 @@
 geomfromgeojson_04|LINESTRING(0 0,1 1)
 geomfromgeojson_05|POLYGON((0 0,1 1,1 0,0 0))
 geomfromgeojson_06|MULTIPOLYGON(((0 0,1 1,1 0,0 0)))
+geomfromgeojson_07|MULTIPOLYGON(((0 0,1 1,1 0,0 0)))
+geomfromgeojson_08|MULTIPOLYGON(((0 0,1 1,1 0,0 0)))
 #1434: Next two errors
 ERROR:  Unable to find 'coordinates' in GeoJSON string
 ERROR:  unexpected character (at offset 0)
@@ -28,3 +30,8 @@
 geomfromgeojson_srs_3|GEOMETRYCOLLECTION(POINT(100 0),LINESTRING(101 0,102 1))
 geomfromgeojson_srs_4|GEOMETRYCOLLECTION(POINT(100 0),LINESTRING(101 0,102 1))
 #3583|MULTIPOLYGON Z (EMPTY)
+ERROR:  The 'coordinates' in GeoJSON polygon are not sufficiently nested
+#4470.a|POLYGON((0 0),())
+ERROR:  The 'coordinates' in GeoJSON polygon are not sufficiently nested
+#4470.c|MULTIPOLYGON(((0 0),()))
+#4470.d|MULTIPOLYGON Z (EMPTY)



More information about the postgis-tickets mailing list