Skip to content

Commit b4ebe84

Browse files
TimHambourgerseanmonstar
authored andcommitted
Fix #354 -- Make advance_mut impl of BufMut for Vec<u8> panic if cnt > remaining
1 parent 8bbe9dd commit b4ebe84

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/buf/buf_mut.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -990,11 +990,13 @@ impl BufMut for Vec<u8> {
990990
unsafe fn advance_mut(&mut self, cnt: usize) {
991991
let len = self.len();
992992
let remaining = self.capacity() - len;
993-
if cnt > remaining {
994-
// Reserve additional capacity, and ensure that the total length
995-
// will not overflow usize.
996-
self.reserve(cnt);
997-
}
993+
994+
assert!(
995+
cnt <= remaining,
996+
"cannot advance past `remaining_mut`: {:?} <= {:?}",
997+
cnt,
998+
remaining
999+
);
9981000

9991001
self.set_len(len + cnt);
10001002
}

tests/test_buf_mut.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ fn test_put_u16() {
4545
}
4646

4747
#[test]
48+
#[should_panic(expected = "cannot advance")]
4849
fn test_vec_advance_mut() {
49-
// Regression test for carllerche/bytes#108.
50+
// Verify fix for #354
5051
let mut buf = Vec::with_capacity(8);
5152
unsafe {
5253
buf.advance_mut(12);
53-
assert_eq!(buf.len(), 12);
54-
assert!(buf.capacity() >= 12, "capacity: {}", buf.capacity());
5554
}
5655
}
5756

0 commit comments

Comments
 (0)