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

VecDeque leaks remaining content when one destructor panics #67232

Closed
jonas-schievink opened this issue Dec 11, 2019 · 1 comment · Fixed by #67235
Closed

VecDeque leaks remaining content when one destructor panics #67232

jonas-schievink opened this issue Dec 11, 2019 · 1 comment · Fixed by #67235
Labels
A-collections Area: `std::collection` C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@jonas-schievink
Copy link
Contributor

STR:

use std::collections::VecDeque;

struct D(u8, bool);

impl Drop for D {
    fn drop(&mut self) {
        println!("Drop {}", self.0);
        if self.1 {
            panic!("panic in `drop`");
        }
    }
}

fn main() {
    let mut q = VecDeque::new();
    q.push_back(D(0, false));
    q.push_back(D(1, false));
    q.push_back(D(2, false));
    q.push_back(D(3, false));
    q.push_back(D(4, false));
    q.push_front(D(100, false));
    q.push_front(D(101, false));
    q.push_front(D(102, true));
}

Output:

Drop 102
thread 'main' panicked at 'panic in `drop`', test.rs:9:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
Drop 101
Drop 100

Caused by the Drop impl of VecDeque:

fn drop(&mut self) {
let (front, back) = self.as_mut_slices();
unsafe {
// use drop for [T]
ptr::drop_in_place(front);
ptr::drop_in_place(back);
}
// RawVec handles deallocation
}

Apparently drop_in_place will continue dropping elements when one destructor panics. That doesn't carry over to the second call, of course.

@jonas-schievink jonas-schievink added A-collections Area: `std::collection` C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Dec 11, 2019
@jonas-schievink
Copy link
Contributor Author

Same goes for vec_deque::IntoIter (it just contains a VecDeque, so would be fixed by fixing the drop impl).

Centril added a commit to Centril/rust that referenced this issue Dec 12, 2019
…drAus

VecDeque: drop remaining items on destructor panic

Closes rust-lang#67232
@bors bors closed this as completed in 48164f8 Dec 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: `std::collection` C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant