Skip to content

Commit 19c7606

Browse files
authored
Merge pull request #119 from paulmach/wkt-panic
encoding/wkt: fix panic for some invalid wkt data
2 parents 07e2162 + 69d851f commit 19c7606

File tree

2 files changed

+247
-0
lines changed

2 files changed

+247
-0
lines changed

encoding/wkt/unmarshal.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ func UnmarshalCollection(s string) (p orb.Collection, err error) {
122122
// trimSpaceBrackets trim space and brackets
123123
func trimSpaceBrackets(s string) string {
124124
s = strings.Trim(s, " ")
125+
if len(s) == 0 {
126+
return ""
127+
}
125128
if s[0] == '(' {
126129
s = s[1:]
127130
}
@@ -187,6 +190,9 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
187190
return orb.Collection{}, nil
188191
}
189192
s = strings.Replace(s, "GEOMETRYCOLLECTION", "", -1)
193+
if len(s) == 0 {
194+
return nil, ErrNotWKT
195+
}
190196
c := orb.Collection{}
191197
ms := splitGeometryCollection(s)
192198
if len(ms) == 0 {

encoding/wkt/unmarshal_test.go

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ func TestTrimSpaceBrackets(t *testing.T) {
1212
s string
1313
expected string
1414
}{
15+
{
16+
name: "empty string",
17+
s: "",
18+
expected: "",
19+
},
20+
{
21+
name: "blank string",
22+
s: " ",
23+
expected: "",
24+
},
1525
{
1626
name: "single point",
1727
s: "(1 2)",
@@ -84,6 +94,39 @@ func TestUnmarshalPoint(t *testing.T) {
8494
}
8595
}
8696

97+
func TestUnmarshalPoint_errors(t *testing.T) {
98+
cases := []struct {
99+
name string
100+
s string
101+
err error
102+
}{
103+
{
104+
name: "just name",
105+
s: "POINT",
106+
err: ErrNotWKT,
107+
},
108+
{
109+
name: "too many points",
110+
s: "POINT(1.34 2.35 3.36)",
111+
err: ErrNotWKT,
112+
},
113+
{
114+
name: "not a point",
115+
s: "MULTIPOINT((1.34 2.35))",
116+
err: ErrIncorrectGeometry,
117+
},
118+
}
119+
120+
for _, tc := range cases {
121+
t.Run(tc.name, func(t *testing.T) {
122+
_, err := UnmarshalPoint(tc.s)
123+
if err != tc.err {
124+
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
125+
}
126+
})
127+
}
128+
}
129+
87130
func TestUnmarshalMultiPoint(t *testing.T) {
88131
cases := []struct {
89132
name string
@@ -123,6 +166,39 @@ func TestUnmarshalMultiPoint(t *testing.T) {
123166
}
124167
}
125168

169+
func TestUnmarshalMultiPoint_errors(t *testing.T) {
170+
cases := []struct {
171+
name string
172+
s string
173+
err error
174+
}{
175+
{
176+
name: "just name",
177+
s: "MULTIPOINT",
178+
err: ErrNotWKT,
179+
},
180+
{
181+
name: "too many points",
182+
s: "MULTIPOINT((1 2),(3 4 5))",
183+
err: ErrNotWKT,
184+
},
185+
{
186+
name: "not a multipoint",
187+
s: "POINT(1 2)",
188+
err: ErrIncorrectGeometry,
189+
},
190+
}
191+
192+
for _, tc := range cases {
193+
t.Run(tc.name, func(t *testing.T) {
194+
_, err := UnmarshalMultiPoint(tc.s)
195+
if err != tc.err {
196+
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
197+
}
198+
})
199+
}
200+
}
201+
126202
func TestUnmarshalLineString(t *testing.T) {
127203
cases := []struct {
128204
name string
@@ -157,6 +233,39 @@ func TestUnmarshalLineString(t *testing.T) {
157233
}
158234
}
159235

236+
func TestUnmarshalLineString_errors(t *testing.T) {
237+
cases := []struct {
238+
name string
239+
s string
240+
err error
241+
}{
242+
{
243+
name: "just name",
244+
s: "LINESTRING",
245+
err: ErrNotWKT,
246+
},
247+
{
248+
name: "too many points",
249+
s: "LINESTRING(1 2,3 4 5)",
250+
err: ErrNotWKT,
251+
},
252+
{
253+
name: "not a multipoint",
254+
s: "POINT(1 2)",
255+
err: ErrIncorrectGeometry,
256+
},
257+
}
258+
259+
for _, tc := range cases {
260+
t.Run(tc.name, func(t *testing.T) {
261+
_, err := UnmarshalLineString(tc.s)
262+
if err != tc.err {
263+
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
264+
}
265+
})
266+
}
267+
}
268+
160269
func TestUnmarshalMultiLineString(t *testing.T) {
161270
cases := []struct {
162271
name string
@@ -191,6 +300,39 @@ func TestUnmarshalMultiLineString(t *testing.T) {
191300
}
192301
}
193302

303+
func TestUnmarshalMultiLineString_errors(t *testing.T) {
304+
cases := []struct {
305+
name string
306+
s string
307+
err error
308+
}{
309+
{
310+
name: "just name",
311+
s: "MULTILINESTRING",
312+
err: ErrNotWKT,
313+
},
314+
{
315+
name: "too many points",
316+
s: "MULTILINESTRING((1 2,3 4 5))",
317+
err: ErrNotWKT,
318+
},
319+
{
320+
name: "not a multi linestring",
321+
s: "POINT(1 2)",
322+
err: ErrIncorrectGeometry,
323+
},
324+
}
325+
326+
for _, tc := range cases {
327+
t.Run(tc.name, func(t *testing.T) {
328+
_, err := UnmarshalMultiLineString(tc.s)
329+
if err != tc.err {
330+
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
331+
}
332+
})
333+
}
334+
}
335+
194336
func TestUnmarshalPolygon(t *testing.T) {
195337
cases := []struct {
196338
name string
@@ -230,6 +372,39 @@ func TestUnmarshalPolygon(t *testing.T) {
230372
}
231373
}
232374

375+
func TestUnmarshalPolygon_errors(t *testing.T) {
376+
cases := []struct {
377+
name string
378+
s string
379+
err error
380+
}{
381+
{
382+
name: "just name",
383+
s: "POLYGON",
384+
err: ErrNotWKT,
385+
},
386+
{
387+
name: "too many points",
388+
s: "POLYGON((1 2,3 4 5))",
389+
err: ErrNotWKT,
390+
},
391+
{
392+
name: "not a polygon",
393+
s: "POINT(1 2)",
394+
err: ErrIncorrectGeometry,
395+
},
396+
}
397+
398+
for _, tc := range cases {
399+
t.Run(tc.name, func(t *testing.T) {
400+
_, err := UnmarshalPolygon(tc.s)
401+
if err != tc.err {
402+
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
403+
}
404+
})
405+
}
406+
}
407+
233408
func TestUnmarshalMutilPolygon(t *testing.T) {
234409
cases := []struct {
235410
name string
@@ -263,6 +438,39 @@ func TestUnmarshalMutilPolygon(t *testing.T) {
263438
}
264439
}
265440

441+
func TestUnmarshalMultiPolygon_errors(t *testing.T) {
442+
cases := []struct {
443+
name string
444+
s string
445+
err error
446+
}{
447+
{
448+
name: "just name",
449+
s: "MULTIPOLYGON",
450+
err: ErrNotWKT,
451+
},
452+
{
453+
name: "too many points",
454+
s: "MULTIPOLYGON(((1 2,3 4 5)))",
455+
err: ErrNotWKT,
456+
},
457+
{
458+
name: "not a multi polygon",
459+
s: "POINT(1 2)",
460+
err: ErrIncorrectGeometry,
461+
},
462+
}
463+
464+
for _, tc := range cases {
465+
t.Run(tc.name, func(t *testing.T) {
466+
_, err := UnmarshalMultiPolygon(tc.s)
467+
if err != tc.err {
468+
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
469+
}
470+
})
471+
}
472+
}
473+
266474
func TestUnmarshalCollection(t *testing.T) {
267475
cases := []struct {
268476
name string
@@ -307,3 +515,36 @@ func TestUnmarshalCollection(t *testing.T) {
307515
})
308516
}
309517
}
518+
519+
func TestUnmarshalCollection_errors(t *testing.T) {
520+
cases := []struct {
521+
name string
522+
s string
523+
err error
524+
}{
525+
{
526+
name: "just name",
527+
s: "GEOMETRYCOLLECTION",
528+
err: ErrNotWKT,
529+
},
530+
{
531+
name: "too many points",
532+
s: "GEOMETRYCOLLECTION(POINT(1 2 3))",
533+
err: ErrNotWKT,
534+
},
535+
{
536+
name: "not a geometry collection",
537+
s: "POINT(1 2)",
538+
err: ErrIncorrectGeometry,
539+
},
540+
}
541+
542+
for _, tc := range cases {
543+
t.Run(tc.name, func(t *testing.T) {
544+
_, err := UnmarshalCollection(tc.s)
545+
if err != tc.err {
546+
t.Fatalf("incorrect error: %v!= %v", err, tc.err)
547+
}
548+
})
549+
}
550+
}

0 commit comments

Comments
 (0)