Skip to content

Commit

Permalink
core: panic on overflow in BorrowedCursor
Browse files Browse the repository at this point in the history
  • Loading branch information
joboet committed Apr 11, 2024
1 parent 05ccc49 commit 91fe6f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions library/core/src/io/borrowed_buf.rs
Expand Up @@ -249,9 +249,10 @@ impl<'a> BorrowedCursor<'a> {
/// Panics if there are less than `n` bytes initialized.
#[inline]
pub fn advance(&mut self, n: usize) -> &mut Self {
assert!(self.buf.init >= self.buf.filled + n);
let filled = self.buf.filled.strict_add(n);
assert!(filled <= self.buf.init);

self.buf.filled += n;
self.buf.filled = filled;
self
}

Expand Down
9 changes: 9 additions & 0 deletions library/std/src/io/tests.rs
Expand Up @@ -209,6 +209,15 @@ fn read_buf_exact() {
assert_eq!(c.read_buf_exact(buf.unfilled()).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
}

#[test]
#[should_panic]
fn borrowed_cursor_advance_overflow() {
let mut buf = [0; 512];
let mut buf = BorrowedBuf::from(&mut buf[..]);
buf.unfilled().advance(1);
buf.unfilled().advance(usize::MAX);
}

#[test]
fn take_eof() {
struct R;
Expand Down

0 comments on commit 91fe6f9

Please sign in to comment.