Skip to content

Commit

Permalink
encoding/wkb: only limit allocation length not read length
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmach committed Mar 9, 2019
1 parent edbcee2 commit 363a051
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
7 changes: 4 additions & 3 deletions encoding/wkb/line_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ func readLineString(r io.Reader, bom binary.ByteOrder) (orb.LineString, error) {
return nil, err
}

if num > maxPointsAlloc {
alloc := num
if alloc > maxPointsAlloc {
// invalid data can come in here and allocate tons of memory.
num = maxPointsAlloc
alloc = maxPointsAlloc
}
result := make(orb.LineString, 0, alloc)

result := make(orb.LineString, 0, num)
for i := 0; i < int(num); i++ {
p, err := readPoint(r, bom)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions encoding/wkb/line_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ var (
)

func TestLineString(t *testing.T) {
large := orb.LineString{}
for i := 0; i < maxPointsAlloc+100; i++ {
large = append(large, orb.Point{float64(i), float64(-i)})
}

cases := []struct {
name string
data []byte
Expand All @@ -26,6 +31,11 @@ func TestLineString(t *testing.T) {
data: testLineStringData,
expected: testLineString,
},
{
name: "large line string",
data: MustMarshal(large),
expected: large,
},
}

for _, tc := range cases {
Expand Down
7 changes: 4 additions & 3 deletions encoding/wkb/point.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ func readMultiPoint(r io.Reader, bom binary.ByteOrder) (orb.MultiPoint, error) {
return nil, err
}

if num > maxPointsAlloc {
alloc := num
if alloc > maxPointsAlloc {
// invalid data can come in here and allocate tons of memory.
num = maxPointsAlloc
alloc = maxPointsAlloc
}
result := make(orb.MultiPoint, 0, alloc)

result := make(orb.MultiPoint, 0, num)
for i := 0; i < int(num); i++ {
byteOrder, typ, err := readByteOrderType(r)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions encoding/wkb/point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ var (
)

func TestMultiPoint(t *testing.T) {
large := orb.MultiPoint{}
for i := 0; i < maxPointsAlloc+100; i++ {
large = append(large, orb.Point{float64(i), float64(-i)})
}

cases := []struct {
name string
data []byte
Expand All @@ -101,6 +106,11 @@ func TestMultiPoint(t *testing.T) {
data: testMultiPointSingleData,
expected: testMultiPointSingle,
},
{
name: "large multi point",
data: MustMarshal(large),
expected: large,
},
}

for _, tc := range cases {
Expand Down
2 changes: 1 addition & 1 deletion encoding/wkb/wkb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
const (
// limits so that bad data can't come in allocate way tons of memory.
// Well formed data with less elements will allocate the correct amount just fine.
maxPointsAlloc = 5000
maxPointsAlloc = 10000
maxMultiAlloc = 100
)

Expand Down

0 comments on commit 363a051

Please sign in to comment.