Skip to content

Commit a97135c

Browse files
author
Or Neeman
committed
rlp: handle case of normal EOF in Stream.readFull()
1 parent 1489c3f commit a97135c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

rlp/decode.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,13 @@ func (s *Stream) readFull(buf []byte) (err error) {
952952
n += nn
953953
}
954954
if err == io.EOF {
955-
err = io.ErrUnexpectedEOF
955+
if n < len(buf) {
956+
err = io.ErrUnexpectedEOF
957+
} else {
958+
// Readers are allowed to give EOF even though the read succeeded.
959+
// In such cases, we discard the EOF, like io.ReadFull() does.
960+
err = nil
961+
}
956962
}
957963
return err
958964
}

rlp/decode_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,26 @@ func TestDecodeWithByteReader(t *testing.T) {
665665
})
666666
}
667667

668+
func testDecodeWithEncReader(t *testing.T, n int) {
669+
s := strings.Repeat("0", n)
670+
_, r, _ := EncodeToReader(s)
671+
var decoded string
672+
err := Decode(r, &decoded)
673+
if err != nil {
674+
t.Errorf("Unexpected decode error with n=%v: %v", n, err)
675+
}
676+
if decoded != s {
677+
t.Errorf("Decode mismatch with n=%v", n)
678+
}
679+
}
680+
681+
// This is a regression test checking that decoding from encReader
682+
// works for RLP values of size 8192 bytes or more.
683+
func TestDecodeWithEncReader(t *testing.T) {
684+
testDecodeWithEncReader(t, 8188) // length with header is 8191
685+
testDecodeWithEncReader(t, 8189) // length with header is 8192
686+
}
687+
668688
// plainReader reads from a byte slice but does not
669689
// implement ReadByte. It is also not recognized by the
670690
// size validation. This is useful to test how the decoder

0 commit comments

Comments
 (0)