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

ReadBytesExt: document the danger of reading after an error #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

eric-seppanen
Copy link

std::io::Read::read_exact documentation says that if an error occurs, the state of the reader is unspecified: it may have consumed some number of bytes (between zero and the size of the buffer).

This makes it unwise to continue reading after an error, since it's not possible to know where the read begins.

A caller may be surprised by this issue, for example by calling read_u16() until it fails, then calling read_u8() to collect a remainder byte. This is not guaranteed to work.

This was specifically observed to behave one way on rust 1.79.0, and then a different way in rust 1.80.0 when using std::io::Cursor as the reader. See #208 for an example of code that broke in 1.80.

Closes #208 (Documents the problem, which is the best we can do.)

std::io::Read::read_exact documentation says that if an error occurs,
the state of the reader is unspecified: it may have consumed some
number of bytes (between zero and the size of the buffer).

This makes it unwise to continue reading after an error, since it's not
possible to know where the read begins.

A caller may be surprised by this issue, for example by calling
read_u16() until it fails, then calling read_u8() to collect a remainder
byte. This is not guaranteed to work.

This was specifically observed to behave one way on rust 1.79.0, and
then a different way in rust 1.80.0 when using std::io::Cursor as the
reader.

Closes BurntSushi#208 (Documents the problem, which is the best we can do.)
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.

Hazard with ReadBytesExt methods and continuing after errors
1 participant