Skip to content

Commit

Permalink
ReadBytesExt: document the danger of reading after an error
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
eric-seppanen committed Aug 2, 2024
1 parent 18f32ca commit a93ba2c
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -67,6 +70,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -94,6 +100,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -121,6 +130,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -148,6 +160,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand All @@ -174,6 +189,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand All @@ -200,6 +218,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand All @@ -226,6 +247,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand All @@ -252,6 +276,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand All @@ -278,6 +305,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand All @@ -304,6 +334,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand All @@ -330,6 +363,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand All @@ -356,6 +392,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -385,6 +424,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand All @@ -411,6 +453,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand All @@ -436,6 +481,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -478,6 +526,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -509,6 +560,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -543,6 +597,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -578,6 +635,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -613,6 +673,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -651,6 +714,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -697,6 +763,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -728,6 +797,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -763,6 +835,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -798,6 +873,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -836,6 +914,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -877,6 +958,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -922,6 +1006,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -962,6 +1049,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down Expand Up @@ -1013,6 +1103,9 @@ pub trait ReadBytesExt: io::Read {
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// If an error is returned, the reader will be left in an unspecified
/// state, and should not be used for additional reads.
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
Expand Down

0 comments on commit a93ba2c

Please sign in to comment.