Skip to content

Commit 645f986

Browse files
committed
core: panic on overflow in BorrowedCursor
1 parent 4b61234 commit 645f986

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

core/src/io/borrowed_buf.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,10 @@ impl<'a> BorrowedCursor<'a> {
249249
/// Panics if there are less than `n` bytes initialized.
250250
#[inline]
251251
pub fn advance(&mut self, n: usize) -> &mut Self {
252-
assert!(self.buf.init >= self.buf.filled + n);
252+
let filled = self.buf.filled.strict_add(n);
253+
assert!(filled <= self.buf.init);
253254

254-
self.buf.filled += n;
255+
self.buf.filled = filled;
255256
self
256257
}
257258

std/src/io/tests.rs

+9
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,15 @@ fn read_buf_exact() {
209209
assert_eq!(c.read_buf_exact(buf.unfilled()).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
210210
}
211211

212+
#[test]
213+
#[should_panic]
214+
fn borrowed_cursor_advance_overflow() {
215+
let mut buf = [0; 512];
216+
let mut buf = BorrowedBuf::from(&mut buf[..]);
217+
buf.unfilled().advance(1);
218+
buf.unfilled().advance(usize::MAX);
219+
}
220+
212221
#[test]
213222
fn take_eof() {
214223
struct R;

0 commit comments

Comments
 (0)