Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Vec.truncate() resilient against failure in Drop #14210

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,14 +635,14 @@ impl<T> Vec<T> {
/// ```
pub fn truncate(&mut self, len: uint) {
unsafe {
let mut i = len;
// drop any extra elements
while i < self.len {
ptr::read(self.as_slice().unsafe_ref(i));
i += 1;
while len < self.len {
// decrement len before the read(), so a failure on Drop doesn't
// re-drop the just-failed value.
self.len -= 1;
ptr::read(self.as_slice().unsafe_ref(self.len));
}
}
self.len = len;
}

/// Work with `self` as a mutable slice.
Expand Down Expand Up @@ -1862,4 +1862,39 @@ mod tests {
assert_eq!(b[0].x, 42);
assert_eq!(b[1].x, 84);
}

#[test]
fn test_vec_truncate_drop() {
static mut drops: uint = 0;
struct Elem(int);
impl Drop for Elem {
fn drop(&mut self) {
unsafe { drops += 1; }
}
}

let mut v = vec![Elem(1), Elem(2), Elem(3), Elem(4), Elem(5)];
assert_eq!(unsafe { drops }, 0);
v.truncate(3);
assert_eq!(unsafe { drops }, 2);
v.truncate(0);
assert_eq!(unsafe { drops }, 5);
}

#[test]
#[should_fail]
fn test_vec_truncate_fail() {
struct BadElem(int);
impl Drop for BadElem {
fn drop(&mut self) {
let BadElem(ref mut x) = *self;
if *x == 0xbadbeef {
fail!("BadElem failure: 0xbadbeef")
}
}
}

let mut v = vec![BadElem(1), BadElem(2), BadElem(0xbadbeef), BadElem(4)];
v.truncate(0);
}
}