BurntSushi / byteorder

Rust library for reading/writing numbers in big-endian and little-endian.
The Unlicense
984 stars 144 forks source link

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

Closed eric-seppanen closed 6 days ago

eric-seppanen commented 4 months ago

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.)