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

Don't leak the recovery closure on filling a hole #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions src/scoped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub struct Hole<'c, 'm, T: 'm, F: FnOnce() -> T> {

impl<'c, 'm, T: 'm, F: FnOnce() -> T> Hole<'c, 'm, T, F> {
/// Fills the Hole.
pub fn fill(self, t: T) {
pub fn fill(mut self, t: T) {
use std::ptr;
use std::mem;

Expand All @@ -121,7 +121,13 @@ impl<'c, 'm, T: 'm, F: FnOnce() -> T> Hole<'c, 'm, T, F> {
}
let num_holes = self.active_holes.get();
self.active_holes.set(num_holes - 1);
// The recovery is the only thing that *might* have a destructor to run. Make sure it is
// not forgotten.
let recovery = self.recovery.take();
mem::forget(self);
// But destroy it after getting rid of self. Otherwise panic in it might trigger destructor
// of us after we already filled the hole which would probably do very weird things.
drop(recovery);
}
}

Expand Down Expand Up @@ -172,4 +178,4 @@ fn panic_on_recovered_panic() {
});
}));
assert!(result.is_err());
}
}