Skip to content

Commit

Permalink
Fix logic error in Buffer::read_more()
Browse files Browse the repository at this point in the history
Buffer::read_more() is supposed to refill the buffer without discarding
its contents, which are in the range `pos .. filled`.

It mistakenly borrows the range `pos ..`, fills that, and then
increments `filled` by the amount read. This overwrites the buffer's
existing contents and sets `filled` to a too-large value that either
exposes uninitialized bytes or walks off the end of the buffer entirely.

This patch makes it correctly fill only the unfilled portion of the
buffer, which should maintain all the type invariants and fix the test
failure introduced in commit b119671.
  • Loading branch information
wgwoods committed Mar 1, 2025
1 parent b119671 commit 6d07144
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions library/std/src/io/buffered/bufreader/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ impl Buffer {

/// Read more bytes into the buffer without discarding any of its contents
pub fn read_more(&mut self, mut reader: impl Read) -> io::Result<usize> {
let mut buf = BorrowedBuf::from(&mut self.buf[self.pos..]);
let old_init = self.initialized - self.pos;
let mut buf = BorrowedBuf::from(&mut self.buf[self.filled..]);
let old_init = self.initialized - self.filled;
unsafe {
buf.set_init(old_init);
}
Expand Down

0 comments on commit 6d07144

Please sign in to comment.