Skip to content

Commit cefd948

Browse files
committed
rlp: reject trailing data when using DecodeBytes
1 parent 593b1b6 commit cefd948

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

‎rlp/decode.go‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,17 @@ func Decode(r io.Reader, val interface{}) error {
110110

111111
// DecodeBytes parses RLP data from b into val.
112112
// Please see the documentation of Decode for the decoding rules.
113+
// The input must contain exactly one value and no trailing data.
113114
func DecodeBytes(b []byte, val interface{}) error {
114115
// TODO: this could use a Stream from a pool.
115-
return NewStream(bytes.NewReader(b), uint64(len(b))).Decode(val)
116+
r := bytes.NewReader(b)
117+
if err := NewStream(r, uint64(len(b))).Decode(val); err != nil {
118+
return err
119+
}
120+
if r.Len() > 0 {
121+
return ErrMoreThanOneValue
122+
}
123+
return nil
116124
}
117125

118126
type decodeError struct {
@@ -517,6 +525,10 @@ var (
517525
ErrElemTooLarge = errors.New("rlp: element is larger than containing list")
518526
ErrValueTooLarge = errors.New("rlp: value size exceeds available input length")
519527

528+
// This error is reported by DecodeBytes if the slice contains
529+
// additional data after the first RLP value.
530+
ErrMoreThanOneValue = errors.New("rlp: input contains more than one value")
531+
520532
// internal errors
521533
errNotInList = errors.New("rlp: call of ListEnd outside of any list")
522534
errNotAtEOL = errors.New("rlp: call of ListEnd not positioned at EOL")

0 commit comments

Comments
 (0)