Skip to content

Commit f816fdc

Browse files
committed
rlp: include target type in decoder error messages
1 parent 6049fcd commit f816fdc

2 files changed

Lines changed: 42 additions & 28 deletions

File tree

‎rlp/decode.go‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ func Decode(r io.Reader, val interface{}) error {
7272
return NewStream(r).Decode(val)
7373
}
7474

75+
type decodeError struct {
76+
msg string
77+
typ reflect.Type
78+
}
79+
80+
func (err decodeError) Error() string {
81+
return fmt.Sprintf("rlp: %s for %v", err.msg, err.typ)
82+
}
83+
7584
func makeNumDecoder(typ reflect.Type) decoder {
7685
kind := typ.Kind()
7786
switch {
@@ -85,17 +94,23 @@ func makeNumDecoder(typ reflect.Type) decoder {
8594
}
8695

8796
func decodeInt(s *Stream, val reflect.Value) error {
88-
num, err := s.uint(val.Type().Bits())
89-
if err != nil {
97+
typ := val.Type()
98+
num, err := s.uint(typ.Bits())
99+
if err == errUintOverflow {
100+
return decodeError{"input string too long", typ}
101+
} else if err != nil {
90102
return err
91103
}
92104
val.SetInt(int64(num))
93105
return nil
94106
}
95107

96108
func decodeUint(s *Stream, val reflect.Value) error {
97-
num, err := s.uint(val.Type().Bits())
98-
if err != nil {
109+
typ := val.Type()
110+
num, err := s.uint(typ.Bits())
111+
if err == errUintOverflow {
112+
return decodeError{"input string too big", typ}
113+
} else if err != nil {
99114
return err
100115
}
101116
val.SetUint(num)
@@ -177,7 +192,7 @@ func decodeList(s *Stream, val reflect.Value, elemdec decoder, maxelem int) erro
177192
i := 0
178193
for {
179194
if i > maxelem {
180-
return fmt.Errorf("rlp: input List has more than %d elements", maxelem)
195+
return decodeError{"input list has too many elements", val.Type()}
181196
}
182197
if val.Kind() == reflect.Slice {
183198
// grow slice if necessary
@@ -228,8 +243,6 @@ func decodeByteSlice(s *Stream, val reflect.Value) error {
228243
return err
229244
}
230245

231-
var errStringDoesntFitArray = errors.New("rlp: string value doesn't fit into target array")
232-
233246
func decodeByteArray(s *Stream, val reflect.Value) error {
234247
kind, size, err := s.Kind()
235248
if err != nil {
@@ -238,14 +251,14 @@ func decodeByteArray(s *Stream, val reflect.Value) error {
238251
switch kind {
239252
case Byte:
240253
if val.Len() == 0 {
241-
return errStringDoesntFitArray
254+
return decodeError{"input string too big", val.Type()}
242255
}
243256
bv, _ := s.Uint()
244257
val.Index(0).SetUint(bv)
245258
zero(val, 1)
246259
case String:
247260
if uint64(val.Len()) < size {
248-
return errStringDoesntFitArray
261+
return decodeError{"input string too big", val.Type()}
249262
}
250263
slice := val.Slice(0, int(size)).Interface().([]byte)
251264
if err := s.readFull(slice); err != nil {
@@ -295,7 +308,7 @@ func makeStructDecoder(typ reflect.Type) (decoder, error) {
295308
}
296309
}
297310
if err = s.ListEnd(); err == errNotAtEOL {
298-
err = errors.New("rlp: input List has too many elements")
311+
err = decodeError{"input list has too many elements", typ}
299312
}
300313
return err
301314
}
@@ -476,6 +489,8 @@ func (s *Stream) Bytes() ([]byte, error) {
476489
}
477490
}
478491

492+
var errUintOverflow = errors.New("rlp: uint overflow")
493+
479494
// Uint reads an RLP string of up to 8 bytes and returns its contents
480495
// as an unsigned integer. If the input does not contain an RLP string, the
481496
// returned error will be ErrExpectedString.
@@ -494,7 +509,7 @@ func (s *Stream) uint(maxbits int) (uint64, error) {
494509
return uint64(s.byteval), nil
495510
case String:
496511
if size > uint64(maxbits/8) {
497-
return 0, fmt.Errorf("rlp: string is larger than %d bits", maxbits)
512+
return 0, errUintOverflow
498513
}
499514
return s.readUint(byte(size))
500515
default:

‎rlp/decode_test.go‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package rlp
33
import (
44
"bytes"
55
"encoding/hex"
6-
"errors"
76
"fmt"
87
"io"
98
"math/big"
@@ -87,7 +86,7 @@ func TestStreamErrors(t *testing.T) {
8786
{"81", calls{"Bytes"}, io.ErrUnexpectedEOF},
8887
{"81", calls{"Uint"}, io.ErrUnexpectedEOF},
8988
{"BFFFFFFFFFFFFFFF", calls{"Bytes"}, io.ErrUnexpectedEOF},
90-
{"89000000000000000001", calls{"Uint"}, errors.New("rlp: string is larger than 64 bits")},
89+
{"89000000000000000001", calls{"Uint"}, errUintOverflow},
9190
{"00", calls{"List"}, ErrExpectedList},
9291
{"80", calls{"List"}, ErrExpectedList},
9392
{"C0", calls{"List", "Uint"}, EOL},
@@ -181,7 +180,7 @@ type decodeTest struct {
181180
input string
182181
ptr interface{}
183182
value interface{}
184-
error error
183+
error string
185184
}
186185

187186
type simplestruct struct {
@@ -214,8 +213,8 @@ var decodeTests = []decodeTest{
214213
{input: "820505", ptr: new(uint32), value: uint32(0x0505)},
215214
{input: "83050505", ptr: new(uint32), value: uint32(0x050505)},
216215
{input: "8405050505", ptr: new(uint32), value: uint32(0x05050505)},
217-
{input: "850505050505", ptr: new(uint32), error: errors.New("rlp: string is larger than 32 bits")},
218-
{input: "C0", ptr: new(uint32), error: ErrExpectedString},
216+
{input: "850505050505", ptr: new(uint32), error: "rlp: input string too big for uint32"},
217+
{input: "C0", ptr: new(uint32), error: ErrExpectedString.Error()},
219218

220219
// slices
221220
{input: "C0", ptr: new([]int), value: []int{}},
@@ -224,25 +223,25 @@ var decodeTests = []decodeTest{
224223
// arrays
225224
{input: "C0", ptr: new([5]int), value: [5]int{}},
226225
{input: "C50102030405", ptr: new([5]int), value: [5]int{1, 2, 3, 4, 5}},
227-
{input: "C6010203040506", ptr: new([5]int), error: errors.New("rlp: input List has more than 5 elements")},
226+
{input: "C6010203040506", ptr: new([5]int), error: "rlp: input list has too many elements for [5]int"},
228227

229228
// byte slices
230229
{input: "01", ptr: new([]byte), value: []byte{1}},
231230
{input: "80", ptr: new([]byte), value: []byte{}},
232231
{input: "8D6162636465666768696A6B6C6D", ptr: new([]byte), value: []byte("abcdefghijklm")},
233232
{input: "C0", ptr: new([]byte), value: []byte{}},
234233
{input: "C3010203", ptr: new([]byte), value: []byte{1, 2, 3}},
235-
{input: "C3820102", ptr: new([]byte), error: errors.New("rlp: string is larger than 8 bits")},
234+
{input: "C3820102", ptr: new([]byte), error: "rlp: input string too big for uint8"},
236235

237236
// byte arrays
238237
{input: "01", ptr: new([5]byte), value: [5]byte{1}},
239238
{input: "80", ptr: new([5]byte), value: [5]byte{}},
240239
{input: "850102030405", ptr: new([5]byte), value: [5]byte{1, 2, 3, 4, 5}},
241240
{input: "C0", ptr: new([5]byte), value: [5]byte{}},
242241
{input: "C3010203", ptr: new([5]byte), value: [5]byte{1, 2, 3, 0, 0}},
243-
{input: "C3820102", ptr: new([5]byte), error: errors.New("rlp: string is larger than 8 bits")},
244-
{input: "86010203040506", ptr: new([5]byte), error: errStringDoesntFitArray},
245-
{input: "850101", ptr: new([5]byte), error: io.ErrUnexpectedEOF},
242+
{input: "C3820102", ptr: new([5]byte), error: "rlp: input string too big for uint8"},
243+
{input: "86010203040506", ptr: new([5]byte), error: "rlp: input string too big for [5]uint8"},
244+
{input: "850101", ptr: new([5]byte), error: io.ErrUnexpectedEOF.Error()},
246245

247246
// byte array reuse (should be zeroed)
248247
{input: "850102030405", ptr: &sharedByteArray, value: [5]byte{1, 2, 3, 4, 5}},
@@ -255,25 +254,25 @@ var decodeTests = []decodeTest{
255254
// zero sized byte arrays
256255
{input: "80", ptr: new([0]byte), value: [0]byte{}},
257256
{input: "C0", ptr: new([0]byte), value: [0]byte{}},
258-
{input: "01", ptr: new([0]byte), error: errStringDoesntFitArray},
259-
{input: "8101", ptr: new([0]byte), error: errStringDoesntFitArray},
257+
{input: "01", ptr: new([0]byte), error: "rlp: input string too big for [0]uint8"},
258+
{input: "8101", ptr: new([0]byte), error: "rlp: input string too big for [0]uint8"},
260259

261260
// strings
262261
{input: "00", ptr: new(string), value: "\000"},
263262
{input: "8D6162636465666768696A6B6C6D", ptr: new(string), value: "abcdefghijklm"},
264-
{input: "C0", ptr: new(string), error: ErrExpectedString},
263+
{input: "C0", ptr: new(string), error: ErrExpectedString.Error()},
265264

266265
// big ints
267266
{input: "01", ptr: new(*big.Int), value: big.NewInt(1)},
268267
{input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*big.Int), value: veryBigInt},
269268
{input: "10", ptr: new(big.Int), value: *big.NewInt(16)}, // non-pointer also works
270-
{input: "C0", ptr: new(*big.Int), error: ErrExpectedString},
269+
{input: "C0", ptr: new(*big.Int), error: ErrExpectedString.Error()},
271270

272271
// structs
273272
{input: "C0", ptr: new(simplestruct), value: simplestruct{0, ""}},
274273
{input: "C105", ptr: new(simplestruct), value: simplestruct{5, ""}},
275274
{input: "C50583343434", ptr: new(simplestruct), value: simplestruct{5, "444"}},
276-
{input: "C3010101", ptr: new(simplestruct), error: errors.New("rlp: input List has too many elements")},
275+
{input: "C3010101", ptr: new(simplestruct), error: "rlp: input list has too many elements for rlp.simplestruct"},
277276
{
278277
input: "C501C302C103",
279278
ptr: new(recstruct),
@@ -312,12 +311,12 @@ func runTests(t *testing.T, decode func([]byte, interface{}) error) {
312311
continue
313312
}
314313
err = decode(input, test.ptr)
315-
if err != nil && test.error == nil {
314+
if err != nil && test.error == "" {
316315
t.Errorf("test %d: unexpected Decode error: %v\ndecoding into %T\ninput %q",
317316
i, err, test.ptr, test.input)
318317
continue
319318
}
320-
if test.error != nil && fmt.Sprint(err) != fmt.Sprint(test.error) {
319+
if test.error != "" && fmt.Sprint(err) != test.error {
321320
t.Errorf("test %d: Decode error mismatch\ngot %v\nwant %v\ndecoding into %T\ninput %q",
322321
i, err, test.error, test.ptr, test.input)
323322
continue

0 commit comments

Comments
 (0)