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

encoding/gob: decoder can return io.EOF on non-zero incomplete input #48905

Closed
mvdan opened this issue Oct 11, 2021 · 1 comment
Closed

encoding/gob: decoder can return io.EOF on non-zero incomplete input #48905

mvdan opened this issue Oct 11, 2021 · 1 comment
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.

Comments

@mvdan
Copy link
Member

mvdan commented Oct 11, 2021

Reproducer: https://play.golang.org/p/q5rwBgfs6_0

The docs say:

If the input is at EOF, Decode returns io.EOF and does not modify e.

I take this to mean that, if the input reader gives 0 bytes and io.EOF, then Decode returns io.EOF without modifying any data. That's fine.

However, as you can see from the playground link, right now the decoder can return io.EOF if the input is cut off at specific non-zero offsets. In particular, if the input has messages for type specifications, but zero messages for data. That triggers the "zero input" io.EOF case documented above, even though we have read some messages.

I encountered this in a real program where the input was being corrupted, so I ended up with non-zero bytes of gob that made the decoder error. I was very surprised to see that, sometimes, the error was io.EOF instead of io.ErrUnexpectedEOF. I narrowed it down to this edge case and reproducer.

I'm going to send a CL with a test and a fix. Another possibility is to modify the docs to clarify the current behavior without changing it. I would personally not agree with that solution, because it seems odd for a decoder to parse a non-zero amount of bytes, hit EOF before it actually decodes any data, and simply give the user an io.EOF error. Especially because Encode always writes a data item, so Decode should require a data item if it can read a non-zero amount of bytes.

In any case, if we wanted to instead document the existing behavior, we could insteady say:

If the input hits EOF before a data item, Decode returns io.EOF and does not modify e.

cc @robpike as per https://dev.golang.org/owners

@mvdan mvdan added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Oct 11, 2021
@gopherbot
Copy link

Change https://golang.org/cl/354972 mentions this issue: encoding/gob: follow documented io.EOF semantics

@golang golang locked and limited conversation to collaborators Oct 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

2 participants