Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/wkt: fix panic for some invalid wkt data #119

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions encoding/wkt/unmarshal.go
Original file line number Diff line number Diff line change
@@ -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:]
}
@@ -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 {
241 changes: 241 additions & 0 deletions encoding/wkt/unmarshal_test.go
Original file line number Diff line number Diff line change
@@ -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)",
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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)
}
})
}
}