-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Protect against stray EOFs #44
base: master
Are you sure you want to change the base?
Conversation
Unfortunately: 1. Read can read bytes and return an EOF at the same time (could cause us to fail to parse something that's actually correct). 2. ReadByte is not allowed to do this (fixed in PeekByte). 3. Returning EOF on _actual_ error cases can cause the error to be silently dropped somewhere up the stack (where the EOF may be interpreted as "I'm done"). I've replaced EOF with ErrUnexpectedEOF where appropriate. This crap really shouldn't be necessary, but such is life.
@@ -74,6 +76,10 @@ func ScanForLinks(br io.Reader, cb func(cid.Cid)) error { | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without knowing this code it is hard to know which functions are safe from returning io.EOFs, for example CborReadHeaderBuf
above does not check for EOFs but from the names and arguments it seems like it could return them. I don't know if this is actually what we should do but here and in discard
it would help me be more sure EOF handling is correct if we did error escalation in a defer statement.
@@ -172,7 +174,7 @@ func (d *Deferred) UnmarshalCBOR(br io.Reader) error { | |||
// Copy the bytes | |||
limitedReader.N = int64(extra) | |||
buf.Grow(int(extra)) | |||
if n, err := buf.ReadFrom(&limitedReader); err != nil { | |||
if n, err := buf.ReadFrom(&limitedReader); err != nil && err != io.EOF { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buf.ReadFrom doesn't return io.EOF: https://pkg.go.dev/[email protected]#Buffer.ReadFrom
Unfortunately:
This crap really shouldn't be necessary, but such is life.