diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index f5ed2a771..ab9ad1844 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -990,11 +990,13 @@ impl BufMut for Vec { 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 f002f7d5f..2e74fb976 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()); } }