Skip to content

Commit

Permalink
Fix tokio-rs#354 -- Make advance_mut impl of BufMut for Vec<u8> panic…
Browse files Browse the repository at this point in the history
… if cnt > remaining
  • Loading branch information
TimHambourger committed Mar 13, 2020
1 parent fe6e673 commit eeee568
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/buf/buf_mut.rs
Expand Up @@ -990,11 +990,13 @@ impl BufMut for Vec<u8> {
unsafe fn advance_mut(&mut self, cnt: usize) {
let len = self.len();
let remaining = self.capacity() - len;
if cnt > remaining {
// Reserve additional capacity, and ensure that the total length
// will not overflow usize.
self.reserve(cnt);
}

assert!(
cnt <= remaining,
"cannot advance past `remaining_mut`: {:?} <= {:?}",
cnt,
remaining
);

self.set_len(len + cnt);
}
Expand Down
5 changes: 2 additions & 3 deletions tests/test_buf_mut.rs
Expand Up @@ -45,13 +45,12 @@ fn test_put_u16() {
}

#[test]
#[should_panic(expected = "cannot advance")]
fn test_vec_advance_mut() {
// Regression test for carllerche/bytes#108.
// Verify fix for #354
let mut buf = Vec::with_capacity(8);
unsafe {
buf.advance_mut(12);
assert_eq!(buf.len(), 12);
assert!(buf.capacity() >= 12, "capacity: {}", buf.capacity());
}
}

Expand Down

0 comments on commit eeee568

Please sign in to comment.