Skip to content

Commit

Permalink
Merge pull request #615 from peterstace/issue_397_check_geometry_coll…
Browse files Browse the repository at this point in the history
…ection_coord_types_for_wkb

Add dimension check when parsing WKB GeometryCollections
  • Loading branch information
peterstace authored May 15, 2024
2 parents 925e936 + 8bc2780 commit d1a3d0b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- Upgrades `golangci-lint` to `v1.58.1`.

- Fixes a bug where geometry collections with mixed coordinate types were
erroneously allowed during WKT parsing.
erroneously allowed during WKT and WKB parsing.

## v0.50.0

Expand Down
7 changes: 7 additions & 0 deletions geom/wkb_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,13 @@ func (p *wkbParser) parseGeometryCollection(ctype CoordinatesType) (GeometryColl
if err != nil {
return GeometryCollection{}, err
}
if geoms[i].CoordinatesType() != ctype {
err := mismatchedGeometryCollectionDimsError{
ctype,
geoms[i].CoordinatesType(),
}
return GeometryCollection{}, err
}
}
return NewGeometryCollection(geoms), nil
}
44 changes: 44 additions & 0 deletions geom/wkb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package geom_test
import (
"bytes"
"encoding/hex"
"fmt"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -678,3 +679,46 @@ func TestWKBUnmarshalEndianess(t *testing.T) {
})
}
}

func TestWKBGeometryCollectionMixedCoordinateTypes(t *testing.T) {
const (
bigEndian = "00"
f64Zero = "0000000000000000"
u32One = "00000001"
)

for _, gc := range []struct {
hex string
ct geom.CoordinatesType
}{
{"00000007", geom.DimXY},
{"000003ef", geom.DimXYZ},
{"000007d7", geom.DimXYM},
{"00000bbf", geom.DimXYZM},
} {
for _, point := range []struct {
hex string
ct geom.CoordinatesType
}{
{"00000001", geom.DimXY},
{"000003e9", geom.DimXYZ},
{"000007d1", geom.DimXYM},
{"00000bb9", geom.DimXYZM},
} {
t.Run(fmt.Sprintf("gc_%v_point_%v", gc.ct, point.ct), func(t *testing.T) {
hex := bigEndian +
gc.hex +
u32One +
bigEndian +
point.hex +
strings.Repeat(f64Zero, point.ct.Dimension())
_, err := geom.UnmarshalWKB(hexStringToBytes(t, hex))
if gc.ct == point.ct {
expectNoErr(t, err)
} else {
expectErr(t, err)
}
})
}
}
}

0 comments on commit d1a3d0b

Please sign in to comment.