From dd1394fd050f713974c3547ef9183a75fb547aba Mon Sep 17 00:00:00 2001 From: tidwall Date: Mon, 18 Dec 2023 12:10:32 -0700 Subject: [PATCH] Fix crash when mixing z-coord dimensionality in a geometry This commit fixes an issue where the parser allows for some points to have a z-coord following coordinates that do not. But then the library will crash when outputing the geometry to geojson. This fix has the parser return the error "invalid coordiantes" when parsed. --- linestring.go | 4 ++++ polygon.go | 4 ++++ polygon_test.go | 15 +++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/linestring.go b/linestring.go index 2190b22..11094e9 100644 --- a/linestring.go +++ b/linestring.go @@ -183,6 +183,10 @@ func parseJSONLineStringCoords( coords = append(coords, geometry.Point{X: nums[0], Y: nums[1]}) if ex == nil { if count > 2 { + if len(coords) > 1 { + err = errCoordinatesInvalid + return false + } ex = new(extra) if count > 3 { ex.dims = 2 diff --git a/polygon.go b/polygon.go index 13c9f98..7813f64 100644 --- a/polygon.go +++ b/polygon.go @@ -231,6 +231,10 @@ func parseJSONPolygonCoords( coords[ii] = append(coords[ii], geometry.Point{X: nums[0], Y: nums[1]}) if ex == nil { if count > 2 { + if len(coords) > 1 || len(coords[ii]) > 1 { + err = errCoordinatesInvalid + return false + } ex = new(extra) if count > 3 { ex.dims = 2 diff --git a/polygon_test.go b/polygon_test.go index 3f23f2f..fea9daa 100644 --- a/polygon_test.go +++ b/polygon_test.go @@ -164,3 +164,18 @@ func TestIssue664(t *testing.T) { } } } + +func TestIssue714(t *testing.T) { + _, err := Parse(`{"type":"Polygon","coordinates":[[[0,0],[10,0],[0,10],[0,0]],[[0,0,0],[0,10,0],[10,0,0],[0,0,0]]]}`, nil) + if err.Error() != "invalid coordinates" { + t.Fatalf("expected '%v', got '%v'", "invalid coordinates", err) + } + _, err = Parse(`{"type":"Polygon","coordinates":[[[0,0],[10,0,1],[0,10],[0,0]]]}`, nil) + if err.Error() != "invalid coordinates" { + t.Fatalf("expected '%v', got '%v'", "invalid coordinates", err) + } + _, err = Parse(`{"type":"LineString","coordinates":[[0,0],[10,0,1],[0,10],[0,0]]}`, nil) + if err.Error() != "invalid coordinates" { + t.Fatalf("expected '%v', got '%v'", "invalid coordinates", err) + } +}