Skip to content

Commit

Permalink
Reject integers w/ appended zero's
Browse files Browse the repository at this point in the history
  • Loading branch information
obscuren committed Apr 4, 2015
1 parent c39484b commit 59597d2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions rlp/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func (err *decodeError) Error() string {

func wrapStreamError(err error, typ reflect.Type) error {
switch err {
case ErrCanonInt:
return &decodeError{msg: "canon int error appends zero's", typ: typ}
case ErrExpectedList:
return &decodeError{msg: "expected input list", typ: typ}
case ErrExpectedString:
Expand Down Expand Up @@ -184,6 +186,12 @@ func decodeBigInt(s *Stream, val reflect.Value) error {
i = new(big.Int)
val.Set(reflect.ValueOf(i))
}

// Reject big integers which are zero appended
if len(b) > 0 && b[0] == 0 {
return wrapStreamError(ErrCanonInt, val.Type())
}

i.SetBytes(b)
return nil
}
Expand Down Expand Up @@ -460,6 +468,7 @@ var (
// Other errors
ErrExpectedString = errors.New("rlp: expected String or Byte")
ErrExpectedList = errors.New("rlp: expected List")
ErrCanonInt = errors.New("rlp: expected Int")
ErrElemTooLarge = errors.New("rlp: element is larger than containing list")

// internal errors
Expand Down
1 change: 1 addition & 0 deletions rlp/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ var decodeTests = []decodeTest{
// big ints
{input: "01", ptr: new(*big.Int), value: big.NewInt(1)},
{input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*big.Int), value: veryBigInt},
{input: "820001", ptr: new(big.Int), error: "rlp: canon int error appends zero's for *big.Int"},
{input: "10", ptr: new(big.Int), value: *big.NewInt(16)}, // non-pointer also works
{input: "C0", ptr: new(*big.Int), error: "rlp: expected input string or byte for *big.Int"},

Expand Down

0 comments on commit 59597d2

Please sign in to comment.