Skip to content

Commit

Permalink
Merge pull request #119 from paulmach/wkt-panic
Browse files Browse the repository at this point in the history
encoding/wkt: fix panic for some invalid wkt data
  • Loading branch information
paulmach authored Jan 6, 2023
2 parents 07e2162 + 69d851f commit 19c7606
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 0 deletions.
6 changes: 6 additions & 0 deletions encoding/wkt/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ func UnmarshalCollection(s string) (p orb.Collection, err error) {
// trimSpaceBrackets trim space and brackets
func trimSpaceBrackets(s string) string {
s = strings.Trim(s, " ")
if len(s) == 0 {
return ""
}
if s[0] == '(' {
s = s[1:]
}
Expand Down Expand Up @@ -187,6 +190,9 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
return orb.Collection{}, nil
}
s = strings.Replace(s, "GEOMETRYCOLLECTION", "", -1)
if len(s) == 0 {
return nil, ErrNotWKT
}
c := orb.Collection{}
ms := splitGeometryCollection(s)
if len(ms) == 0 {
Expand Down
241 changes: 241 additions & 0 deletions encoding/wkt/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ func TestTrimSpaceBrackets(t *testing.T) {
s string
expected string
}{
{
name: "empty string",
s: "",
expected: "",
},
{
name: "blank string",
s: " ",
expected: "",
},
{
name: "single point",
s: "(1 2)",
Expand Down Expand Up @@ -84,6 +94,39 @@ func TestUnmarshalPoint(t *testing.T) {
}
}

func TestUnmarshalPoint_errors(t *testing.T) {
cases := []struct {
name string
s string
err error
}{
{
name: "just name",
s: "POINT",
err: ErrNotWKT,
},
{
name: "too many points",
s: "POINT(1.34 2.35 3.36)",
err: ErrNotWKT,
},
{
name: "not a point",
s: "MULTIPOINT((1.34 2.35))",
err: ErrIncorrectGeometry,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := UnmarshalPoint(tc.s)
if err != tc.err {
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
}
})
}
}

func TestUnmarshalMultiPoint(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -123,6 +166,39 @@ func TestUnmarshalMultiPoint(t *testing.T) {
}
}

func TestUnmarshalMultiPoint_errors(t *testing.T) {
cases := []struct {
name string
s string
err error
}{
{
name: "just name",
s: "MULTIPOINT",
err: ErrNotWKT,
},
{
name: "too many points",
s: "MULTIPOINT((1 2),(3 4 5))",
err: ErrNotWKT,
},
{
name: "not a multipoint",
s: "POINT(1 2)",
err: ErrIncorrectGeometry,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := UnmarshalMultiPoint(tc.s)
if err != tc.err {
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
}
})
}
}

func TestUnmarshalLineString(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -157,6 +233,39 @@ func TestUnmarshalLineString(t *testing.T) {
}
}

func TestUnmarshalLineString_errors(t *testing.T) {
cases := []struct {
name string
s string
err error
}{
{
name: "just name",
s: "LINESTRING",
err: ErrNotWKT,
},
{
name: "too many points",
s: "LINESTRING(1 2,3 4 5)",
err: ErrNotWKT,
},
{
name: "not a multipoint",
s: "POINT(1 2)",
err: ErrIncorrectGeometry,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := UnmarshalLineString(tc.s)
if err != tc.err {
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
}
})
}
}

func TestUnmarshalMultiLineString(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -191,6 +300,39 @@ func TestUnmarshalMultiLineString(t *testing.T) {
}
}

func TestUnmarshalMultiLineString_errors(t *testing.T) {
cases := []struct {
name string
s string
err error
}{
{
name: "just name",
s: "MULTILINESTRING",
err: ErrNotWKT,
},
{
name: "too many points",
s: "MULTILINESTRING((1 2,3 4 5))",
err: ErrNotWKT,
},
{
name: "not a multi linestring",
s: "POINT(1 2)",
err: ErrIncorrectGeometry,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := UnmarshalMultiLineString(tc.s)
if err != tc.err {
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
}
})
}
}

func TestUnmarshalPolygon(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -230,6 +372,39 @@ func TestUnmarshalPolygon(t *testing.T) {
}
}

func TestUnmarshalPolygon_errors(t *testing.T) {
cases := []struct {
name string
s string
err error
}{
{
name: "just name",
s: "POLYGON",
err: ErrNotWKT,
},
{
name: "too many points",
s: "POLYGON((1 2,3 4 5))",
err: ErrNotWKT,
},
{
name: "not a polygon",
s: "POINT(1 2)",
err: ErrIncorrectGeometry,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := UnmarshalPolygon(tc.s)
if err != tc.err {
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
}
})
}
}

func TestUnmarshalMutilPolygon(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -263,6 +438,39 @@ func TestUnmarshalMutilPolygon(t *testing.T) {
}
}

func TestUnmarshalMultiPolygon_errors(t *testing.T) {
cases := []struct {
name string
s string
err error
}{
{
name: "just name",
s: "MULTIPOLYGON",
err: ErrNotWKT,
},
{
name: "too many points",
s: "MULTIPOLYGON(((1 2,3 4 5)))",
err: ErrNotWKT,
},
{
name: "not a multi polygon",
s: "POINT(1 2)",
err: ErrIncorrectGeometry,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := UnmarshalMultiPolygon(tc.s)
if err != tc.err {
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
}
})
}
}

func TestUnmarshalCollection(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -307,3 +515,36 @@ func TestUnmarshalCollection(t *testing.T) {
})
}
}

func TestUnmarshalCollection_errors(t *testing.T) {
cases := []struct {
name string
s string
err error
}{
{
name: "just name",
s: "GEOMETRYCOLLECTION",
err: ErrNotWKT,
},
{
name: "too many points",
s: "GEOMETRYCOLLECTION(POINT(1 2 3))",
err: ErrNotWKT,
},
{
name: "not a geometry collection",
s: "POINT(1 2)",
err: ErrIncorrectGeometry,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := UnmarshalCollection(tc.s)
if err != tc.err {
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
}
})
}
}

0 comments on commit 19c7606

Please sign in to comment.