Skip to content

Commit 6788f95

Browse files
committed
rlp: fix handling of single byte zero when decoding into a pointer
A single zero byte carries information and should not set the pointer to nil. This is arguably a corner case. While here, fix the comment to explain pointer reuse.
1 parent 509d0a8 commit 6788f95

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

‎rlp/decode.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ type Decoder interface {
3737
// DecodeRLP.
3838
//
3939
// To decode into a pointer, Decode will set the pointer to nil if the
40-
// input has size zero or the input is a single byte with value zero.
41-
// If the input has nonzero size, Decode will allocate a new value of
42-
// the type being pointed to.
40+
// input has size zero. If the input has nonzero size, Decode will
41+
// parse the input data into a value of the type being pointed to.
42+
// If the pointer is non-nil, the existing value will reused.
4343
//
4444
// To decode into a struct, Decode expects the input to be an RLP
4545
// list. The decoded elements of the list are assigned to each public
@@ -382,8 +382,8 @@ func makePtrDecoder(typ reflect.Type) (decoder, error) {
382382
return nil, err
383383
}
384384
dec := func(s *Stream, val reflect.Value) (err error) {
385-
_, size, err := s.Kind()
386-
if err != nil || size == 0 && s.byteval == 0 {
385+
kind, size, err := s.Kind()
386+
if err != nil || size == 0 && kind != Byte {
387387
// rearm s.Kind. This is important because the input
388388
// position must advance to the next value even though
389389
// we don't read anything.

‎rlp/decode_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ var decodeTests = []decodeTest{
378378
},
379379

380380
// pointers
381-
{input: "00", ptr: new(*uint), value: (*uint)(nil)},
381+
{input: "00", ptr: new(*uint), value: uintp(0)},
382382
{input: "80", ptr: new(*uint), value: (*uint)(nil)},
383383
{input: "C0", ptr: new(*uint), value: (*uint)(nil)},
384384
{input: "07", ptr: new(*uint), value: uintp(7)},

0 commit comments

Comments
 (0)