Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Protect against stray EOFs #44

wants to merge 2 commits into from

Conversation

Stebalien
Copy link
Collaborator

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.

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.
@Stebalien Stebalien requested a review from magik6k March 3, 2021 22:10
@@ -74,6 +76,10 @@ func ScanForLinks(br io.Reader, cb func(cid.Cid)) error {
}
Copy link
Collaborator

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 {
Copy link
Contributor

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/bytes@go1.17.6#Buffer.ReadFrom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants