Skip to content

Commit

Permalink
Allow set_len(0) on an empty ThinVec.
Browse files Browse the repository at this point in the history
Currently it crashes.
  • Loading branch information
nnethercote committed Aug 17, 2022
1 parent 1d06720 commit 6f53b2f
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,13 @@ impl<T> ThinVec<T> {
}

pub unsafe fn set_len(&mut self, len: usize) {
self.header_mut().set_len(len)
if self.capacity() == 0 {
// A prerequisite of `Vec::set_len` is that `new_len` must be
// less than or equal to capacity(). The same applies here.
assert!(len == 0, "invalid set_len({}) on empty ThinVec", len);
} else {
self.header_mut().set_len(len)
}
}

pub fn push(&mut self, val: T) {
Expand Down Expand Up @@ -2749,4 +2755,21 @@ mod std_tests {
assert_eq!(padding::<Funky<[*mut usize; 1024]>>(), 128 - HEADER_SIZE);
assert_aligned_head_ptr!(Funky<[*mut usize; 1024]>);
}

#[test]
fn test_set_len() {
let mut vec: ThinVec<u32> = thin_vec![];
unsafe {
vec.set_len(0); // at one point this caused a crash
}
}

#[test]
#[should_panic(expected = "invalid set_len(1) on empty ThinVec")]
fn test_set_len_invalid() {
let mut vec: ThinVec<u32> = thin_vec![];
unsafe {
vec.set_len(1);
}
}
}

0 comments on commit 6f53b2f

Please sign in to comment.