Skip to content

Commit 67dc2fd

Browse files
committed
Fix logic error in Buffer::read_more()
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.
1 parent c4a6461 commit 67dc2fd

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

std/src/io/buffered/bufreader/buffer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ impl Buffer {
109109

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

0 commit comments

Comments
 (0)