Skip to content

Commit 7180699

Browse files
committed
rlp: require declared number of input elements for array types
1 parent 9c7281c commit 7180699

2 files changed

Lines changed: 26 additions & 40 deletions

File tree

‎rlp/decode.go‎

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ type Decoder interface {
5959
//
6060
// To decode into a slice, the input must be a list and the resulting
6161
// slice will contain the input elements in order. For byte slices,
62-
// the input must be an RLP string.
62+
// the input must be an RLP string. Array types decode similarly, with
63+
// the additional restriction that the number of input elements (or
64+
// bytes) must match the array's length.
6365
//
6466
// To decode into a Go string, the input must be an RLP string. The
6567
// input bytes are taken as-is and will not necessarily be valid UTF-8.
@@ -279,19 +281,10 @@ func decodeListSlice(s *Stream, val reflect.Value, elemdec decoder) error {
279281
}
280282

281283
func decodeListArray(s *Stream, val reflect.Value, elemdec decoder) error {
282-
size, err := s.List()
284+
_, err := s.List()
283285
if err != nil {
284286
return wrapStreamError(err, val.Type())
285287
}
286-
if size == 0 {
287-
zero(val, 0)
288-
return s.ListEnd()
289-
}
290-
291-
// The approach here is stolen from package json, although we differ
292-
// in the semantics for arrays. package json discards remaining
293-
// elements that would not fit into the array. We generate an error in
294-
// this case because we'd be losing information.
295288
vlen := val.Len()
296289
i := 0
297290
for ; i < vlen; i++ {
@@ -302,7 +295,7 @@ func decodeListArray(s *Stream, val reflect.Value, elemdec decoder) error {
302295
}
303296
}
304297
if i < vlen {
305-
zero(val, i)
298+
return &decodeError{msg: "input list has too few elements", typ: val.Type()}
306299
}
307300
return wrapStreamError(s.ListEnd(), val.Type())
308301
}
@@ -321,23 +314,28 @@ func decodeByteArray(s *Stream, val reflect.Value) error {
321314
if err != nil {
322315
return err
323316
}
317+
vlen := val.Len()
324318
switch kind {
325319
case Byte:
326-
if val.Len() == 0 {
320+
if vlen == 0 {
327321
return &decodeError{msg: "input string too long", typ: val.Type()}
328322
}
323+
if vlen > 1 {
324+
return &decodeError{msg: "input string too short", typ: val.Type()}
325+
}
329326
bv, _ := s.Uint()
330327
val.Index(0).SetUint(bv)
331-
zero(val, 1)
332328
case String:
333-
if uint64(val.Len()) < size {
329+
if uint64(vlen) < size {
334330
return &decodeError{msg: "input string too long", typ: val.Type()}
335331
}
336-
slice := val.Slice(0, int(size)).Interface().([]byte)
332+
if uint64(vlen) > size {
333+
return &decodeError{msg: "input string too short", typ: val.Type()}
334+
}
335+
slice := val.Slice(0, vlen).Interface().([]byte)
337336
if err := s.readFull(slice); err != nil {
338337
return err
339338
}
340-
zero(val, int(size))
341339
// Reject cases where single byte encoding should have been used.
342340
if size == 1 && slice[0] < 56 {
343341
return wrapStreamError(ErrCanonSize, val.Type())
@@ -348,14 +346,6 @@ func decodeByteArray(s *Stream, val reflect.Value) error {
348346
return nil
349347
}
350348

351-
func zero(val reflect.Value, start int) {
352-
z := reflect.Zero(val.Type().Elem())
353-
end := val.Len()
354-
for i := start; i < end; i++ {
355-
val.Index(i).Set(z)
356-
}
357-
}
358-
359349
func makeStructDecoder(typ reflect.Type) (decoder, error) {
360350
fields, err := structFields(typ)
361351
if err != nil {

‎rlp/decode_test.go‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,6 @@ var (
290290
)
291291
)
292292

293-
var (
294-
sharedByteArray [5]byte
295-
sharedPtr = new(*uint)
296-
)
297-
298293
var decodeTests = []decodeTest{
299294
// integers
300295
{input: "05", ptr: new(uint32), value: uint32(5)},
@@ -315,11 +310,16 @@ var decodeTests = []decodeTest{
315310
{input: "F8020004", ptr: new([]uint), error: "rlp: non-canonical size information for []uint"},
316311

317312
// arrays
318-
{input: "C0", ptr: new([5]uint), value: [5]uint{}},
319313
{input: "C50102030405", ptr: new([5]uint), value: [5]uint{1, 2, 3, 4, 5}},
314+
{input: "C0", ptr: new([5]uint), error: "rlp: input list has too few elements for [5]uint"},
315+
{input: "C102", ptr: new([5]uint), error: "rlp: input list has too few elements for [5]uint"},
320316
{input: "C6010203040506", ptr: new([5]uint), error: "rlp: input list has too many elements for [5]uint"},
321317
{input: "F8020004", ptr: new([5]uint), error: "rlp: non-canonical size information for [5]uint"},
322318

319+
// zero sized arrays
320+
{input: "C0", ptr: new([0]uint), value: [0]uint{}},
321+
{input: "C101", ptr: new([0]uint), error: "rlp: input list has too many elements for [0]uint"},
322+
323323
// byte slices
324324
{input: "01", ptr: new([]byte), value: []byte{1}},
325325
{input: "80", ptr: new([]byte), value: []byte{}},
@@ -328,21 +328,17 @@ var decodeTests = []decodeTest{
328328
{input: "8105", ptr: new([]byte), error: "rlp: non-canonical size information for []uint8"},
329329

330330
// byte arrays
331-
{input: "01", ptr: new([5]byte), value: [5]byte{1}},
332-
{input: "80", ptr: new([5]byte), value: [5]byte{}},
331+
{input: "02", ptr: new([1]byte), value: [1]byte{2}},
333332
{input: "850102030405", ptr: new([5]byte), value: [5]byte{1, 2, 3, 4, 5}},
334333

335334
// byte array errors
335+
{input: "02", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
336+
{input: "80", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
337+
{input: "820000", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
336338
{input: "C0", ptr: new([5]byte), error: "rlp: expected input string or byte for [5]uint8"},
337339
{input: "C3010203", ptr: new([5]byte), error: "rlp: expected input string or byte for [5]uint8"},
338340
{input: "86010203040506", ptr: new([5]byte), error: "rlp: input string too long for [5]uint8"},
339-
{input: "8105", ptr: new([5]byte), error: "rlp: non-canonical size information for [5]uint8"},
340-
341-
// byte array reuse (should be zeroed)
342-
{input: "850102030405", ptr: &sharedByteArray, value: [5]byte{1, 2, 3, 4, 5}},
343-
{input: "01", ptr: &sharedByteArray, value: [5]byte{1}}, // kind: String
344-
{input: "850102030405", ptr: &sharedByteArray, value: [5]byte{1, 2, 3, 4, 5}},
345-
{input: "01", ptr: &sharedByteArray, value: [5]byte{1}}, // kind: Byte
341+
{input: "8105", ptr: new([1]byte), error: "rlp: non-canonical size information for [1]uint8"},
346342

347343
// zero sized byte arrays
348344
{input: "80", ptr: new([0]byte), value: [0]byte{}},

0 commit comments

Comments
 (0)