Rollup merge of #123806 - joboet:advanced_overflow, r=Amanieu
Panic on overflow in `BorrowedCursor::advance` Passing `usize::MAX` to `advance` clearly isn't correct, but the current assertion fails to detect this when overflow checks are disabled. This isn't unsound, but should probably be fixed regardless.
This commit is contained in:
commit
d8ae975c02
@ -249,9 +249,10 @@ pub unsafe fn as_mut(&mut self) -> &mut [MaybeUninit<u8>] {
|
|||||||
/// Panics if there are less than `n` bytes initialized.
|
/// Panics if there are less than `n` bytes initialized.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn advance(&mut self, n: usize) -> &mut Self {
|
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
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,6 +209,15 @@ fn read_buf_exact() {
|
|||||||
assert_eq!(c.read_buf_exact(buf.unfilled()).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
|
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]
|
#[test]
|
||||||
fn take_eof() {
|
fn take_eof() {
|
||||||
struct R;
|
struct R;
|
||||||
|
Loading…
Reference in New Issue
Block a user