Skip to content

Commit

Permalink
Check for max allocation (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianshih1 authored Apr 20, 2024
1 parent 7a2eb5f commit 0b21284
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions codec_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ type arrayDecoder struct {
decoder ValDecoder
}

// Max allocation size for an array due to the limit in number of bits in a heap address:
// https://github.com/golang/go/blob/7f76c00fc5678fa782708ba8fece63750cb89d03/src/runtime/malloc.go#L183
var maxAllocSize = uint64(1 << 48)

func (d *arrayDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
var size int
sliceType := d.typ
Expand All @@ -51,6 +55,10 @@ func (d *arrayDecoder) Decode(ptr unsafe.Pointer, r *Reader) {

start := size
size += int(l)
if size > int(maxAllocSize) {
r.ReportError("decode array", "size exceeded max allocation size")
return
}
sliceType.UnsafeGrow(ptr, size)

for i := start; i < size; i++ {
Expand Down
13 changes: 13 additions & 0 deletions decoder_array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,16 @@ func TestDecoder_ArraySliceItemError(t *testing.T) {

assert.Error(t, err)
}

func TestDecoder_ArrayMaxAllocationError(t *testing.T) {
defer ConfigTeardown()
data := []byte{0x2, 0x0, 0xe9, 0xe9, 0xe9, 0xe9, 0xe9, 0xe9, 0xe9, 0xe9, 0x0}
schema := `{"type":"array", "items": { "type": "boolean" }}`
dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
require.NoError(t, err)

var got []bool
err = dec.Decode(&got)

assert.Error(t, err)
}

0 comments on commit 0b21284

Please sign in to comment.