From a93ba2cb2d2c94eaf07c862388ebefbe3fff50df Mon Sep 17 00:00:00 2001 From: Eric Seppanen Date: Fri, 2 Aug 2024 16:26:22 -0700 Subject: [PATCH] ReadBytesExt: document the danger of reading after an error 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 #208 (Documents the problem, which is the best we can do.) --- src/io.rs | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/src/io.rs b/src/io.rs index dfad2ca..c531311 100644 --- a/src/io.rs +++ b/src/io.rs @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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