From 30bd7c1f21b52618d4bf5fa84986629e63385c33 Mon Sep 17 00:00:00 2001 From: Dan Burkert <dan@danburkert.com> Date: Sun, 30 Apr 2017 16:14:54 -0700 Subject: [PATCH] Vec::advance_mut can advance past the end of the buffer (#108) --- src/buf/buf_mut.rs | 2 +- tests/test_buf_mut.rs | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index de576a5..b03103a 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -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); diff --git a/tests/test_buf_mut.rs b/tests/test_buf_mut.rs index fe2e9c8..896e31d 100644 --- a/tests/test_buf_mut.rs +++ b/tests/test_buf_mut.rs @@ -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); -- GitLab