Skip to content

Commit

Permalink
Fix crash when mixing z-coord dimensionality in a geometry
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tidwall committed Dec 18, 2023
1 parent ad814f6 commit dd1394f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions linestring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions polygon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit dd1394f

Please sign in to comment.