From 4429c4f622a791122852cbcb91aaea476788a5da Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 15 Jun 2019 16:19:13 +0900 Subject: [PATCH] Panic guard for increase_refcount --- futures-util/src/task/waker.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/futures-util/src/task/waker.rs b/futures-util/src/task/waker.rs index 5fb2f9210a..ffed78955c 100644 --- a/futures-util/src/task/waker.rs +++ b/futures-util/src/task/waker.rs @@ -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(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 = 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`