Vec::advance_mut can advance past the end of the buffer (#108)

This commit is contained in:
Dan Burkert
2017-04-30 16:14:54 -07:00
committed by Carl Lerche
parent 923d927bd1
commit 30bd7c1f21
2 changed files with 12 additions and 1 deletions
+1 -1
View File
@@ -713,7 +713,7 @@ impl BufMut for Vec<u8> {
if cnt > remaining {
// Reserve additional capacity, and ensure that the total length
// will not overflow usize.
self.reserve(cnt - remaining);
self.reserve(cnt);
}
self.set_len(len + cnt);
+11
View File
@@ -49,6 +49,17 @@ fn test_put_u16() {
assert_eq!(b"\x54\x21", &buf[..]);
}
#[test]
fn test_vec_advance_mut() {
// Regression test for carllerche/bytes#108.
let mut buf = Vec::with_capacity(8);
unsafe {
buf.advance_mut(12);
assert_eq!(buf.len(), 12);
assert!(buf.capacity() >= 12, "capacity: {}", buf.capacity());
}
}
#[test]
fn test_clone() {
let mut buf = BytesMut::with_capacity(100);