aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Tim Hambourger <tim.hambourger@gmail.com> 2020-03-13 16:50:04 -0500
committerGravatar Sean McArthur <sean@seanmonstar.com> 2020-03-25 12:30:15 -0700
commitb4ebe8432ec7e90cc4a4b5ccfb2c9e131bda3a36 (patch)
tree449e0e76056d1ba59993e7c0e001c912d6347c75
parent8bbe9dd87bdf11b1536e56415a39827eeb6d0f3e (diff)
downloadbytes-b4ebe8432ec7e90cc4a4b5ccfb2c9e131bda3a36.tar.gz
bytes-b4ebe8432ec7e90cc4a4b5ccfb2c9e131bda3a36.tar.zst
bytes-b4ebe8432ec7e90cc4a4b5ccfb2c9e131bda3a36.zip
Fix #354 -- Make advance_mut impl of BufMut for Vec<u8> panic if cnt > remaining
-rw-r--r--src/buf/buf_mut.rs12
-rw-r--r--tests/test_buf_mut.rs5
2 files changed, 9 insertions, 8 deletions
diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs
index f5ed2a7..ab9ad18 100644
--- a/src/buf/buf_mut.rs
+++ b/src/buf/buf_mut.rs
@@ -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);
}
diff --git a/tests/test_buf_mut.rs b/tests/test_buf_mut.rs
index f002f7d..2e74fb9 100644
--- a/tests/test_buf_mut.rs
+++ b/tests/test_buf_mut.rs
@@ -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());
}
}