Skip to content

Commit

Permalink
decoder: add slice len bounds checks
Browse files Browse the repository at this point in the history
  • Loading branch information
riptl committed Aug 20, 2022
1 parent 990fbf1 commit f999127
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions decoder_bin.go
Expand Up @@ -20,6 +20,7 @@ package bin
import (
"encoding/binary"
"fmt"
"io"
"reflect"

"go.uber.org/zap"
Expand Down Expand Up @@ -181,6 +182,10 @@ func (dec *Decoder) decodeBin(rv reflect.Value, opt *option) (err error) {
zlog.Debug("reading slice", zap.Int("len", l), typeField("type", rv))
}

if l > dec.Remaining() {
return io.ErrUnexpectedEOF
}

rv.Set(reflect.MakeSlice(rt, l, l))
for i := 0; i < l; i++ {
if err = dec.decodeBin(rv.Index(i), nil); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions decoder_borsh.go
Expand Up @@ -20,6 +20,7 @@ package bin
import (
"errors"
"fmt"
"io"
"reflect"

"go.uber.org/zap"
Expand Down Expand Up @@ -200,6 +201,9 @@ func (dec *Decoder) decodeBorsh(rv reflect.Value, opt *option) (err error) {
// Empty slices are left nil
return
}
if l > dec.Remaining() {
return io.ErrUnexpectedEOF
}

rv.Set(reflect.MakeSlice(rt, l, l))
for i := 0; i < l; i++ {
Expand Down
5 changes: 5 additions & 0 deletions decoder_compact-u16.go
Expand Up @@ -19,6 +19,7 @@ package bin

import (
"fmt"
"io"
"reflect"

"go.uber.org/zap"
Expand Down Expand Up @@ -179,6 +180,10 @@ func (dec *Decoder) decodeCompactU16(rv reflect.Value, opt *option) (err error)
zlog.Debug("reading slice", zap.Int("len", l), typeField("type", rv))
}

if l > dec.Remaining() {
return io.ErrUnexpectedEOF
}

rv.Set(reflect.MakeSlice(rt, l, l))
for i := 0; i < l; i++ {
if err = dec.decodeCompactU16(rv.Index(i), nil); err != nil {
Expand Down

0 comments on commit f999127

Please sign in to comment.