Skip to content

Commit

Permalink
Panic guard for increase_refcount
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jun 15, 2019
1 parent 572afe7 commit 4429c4f
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions futures-util/src/task/waker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,29 @@ where
}
}

// FIXME: panics on Arc::clone / refcount changes could wreak havoc on the
// code here. We should guard against this by aborting.

unsafe fn increase_refcount<T: ArcWake>(data: *const ()) {
struct Guard;

impl Drop for Guard {
fn drop(&mut self) {
// panicking twice to abort the program
panic!("panics on Arc::clone / refcount changes")
}
}

// Panics on Arc::clone / refcount changes could wreak havoc on the
// code here. Guard against this by aborting.
let guard = Guard;

// Retain Arc by creating a copy
let arc: Arc<T> = Arc::from_raw(data as *const T);
let arc_clone = arc.clone();
// Forget the Arcs again, so that the refcount isn't decrased
mem::forget(arc);
mem::forget(arc_clone);

// Forget the guard to avoid panicking.
mem::forget(guard);
}

// used by `waker_ref`
Expand Down

0 comments on commit 4429c4f

Please sign in to comment.