You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nothing here is unsound, just surprising. I tried this code:
use std::io::Read;structLieLieLie(bool);implReadforLieLieLie{fnread(&mutself,buf:&mut[u8]) -> std::io::Result<usize>{if core::mem::take(&mutself.0){// Take my hand and let's end it allOk(buf.len() + 1)}else{Ok(buf.len())}}}fnmain(){letmut buffer = vec![0;4];letmut reader = LieLieLie(true).take(4);// Primed the `Limit` by lying about the read size.let _ = reader.read(&mut buffer[..]);// Oops, limit is now u64::MAX.
reader.read_to_end(&mut buffer);}
I expected to see this happen: The wrapping into Take ensures that no more than the specified number are appended to the underlying vector.
Instead, this happened: read_to_end enters a very long loop, eventually fails to allocate and crashes.
impl<T:Read>ReadforTake<T>{fn read(&mutself,buf:&mut[u8]) -> Result<usize>{// …let n = self.inner.read(&mut buf[..max])?;self.limit -= n asu64;
When the inner: T is misbehaved then n may end up larger than max, causing a wrapping subtraction. A remedy may be changing this to a saturating subtraction.
The text was updated successfully, but these errors were encountered:
This is especially surprising because BufRead::consumes protects against this kind of misuse by the caller:
fnconsume(&mutself,amt:usize){// Don't let callers reset the limit by passing an overlarge valuelet amt = cmp::min(amt asu64,self.limit)asusize;self.limit -= amt asu64;self.inner.consume(amt);}
Nothing here is unsound, just surprising. I tried this code:
I expected to see this happen: The wrapping into
Take
ensures that no more than the specified number are appended to the underlying vector.Instead, this happened:
read_to_end
enters a very long loop, eventually fails to allocate and crashes.Meta
rustc --version --verbose
:The standard library includes this code:
rust/library/std/src/io/mod.rs
Lines 2561 to 2562 in 83460d5
When the
inner: T
is misbehaved thenn
may end up larger thanmax
, causing a wrapping subtraction. A remedy may be changing this to a saturating subtraction.The text was updated successfully, but these errors were encountered: