rlp: handle case of normal EOF in Stream.readFull() #22336
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@fjl (since you appear a lot in the rlp git history): This fixes a bug resulting from the combination of three factors:
encReader.Read()
returningEOF
when a successful read (i.e. wherelen(b)
bytes were read) reaches the end. This, in itself, is not a bug, since the docs forio.Reader
say returning EOF in such cases is allowed (but not required). Seego-ethereum/rlp/encode.go
Lines 271 to 280 in 1489c3f
Stream.readFull()
givingErrUnexpectedEOF
when it gets anEOF
, even if the read was successful (i.e. iflen(buff)
bytes were read). Seego-ethereum/rlp/decode.go
Lines 954 to 956 in 1489c3f
bufio.Reader.Read()
in whether or not it passes on the error from its underlying reader if the number of bytes read is non-zero. Namely, if the buffer is empty and the amount to read is greater than the buffer size (4096 bytes by default), the resulting EOF will be passed on, but otherwise the read will return nil for its error and the EOF from the underlying reader would only be returned on the next call to.Read()
. See https://github.com/golang/go/blob/5faf941df067b33485edb9cd2e880869e7feb6a3/src/bufio/bufio.go#L221 and https://github.com/golang/go/blob/5faf941df067b33485edb9cd2e880869e7feb6a3/src/bufio/bufio.go#L231-L242.The result of this is that if you encode a large value using
rlp.EncodeToReader
, you can then get an error leading to failure in decoding. I've added a test in this PR which fails without the fix. Specifically, it fails when the message is 8192 bytes or greater, but passes if it's less (which has to do with the specific reads done during decoding). If you'd rather use a different sort of test, I'd be happy to change it.The PR fixes this bug by making
Stream.readFull()
tolerate the EOF error if we read the number of bytes we were supposed to, bringing its behavior more in line with what is done inio.ReadFull()
(https://github.com/golang/go/blob/5faf941df067b33485edb9cd2e880869e7feb6a3/src/io/io.go#L314-L348).A different way to fix this would have been to modify
encReader()
to not returnEOF
in such cases.