From 73d6679aad3b8f76cfe45666273925d03382f712 Mon Sep 17 00:00:00 2001 From: Arthur Carcano Date: Fri, 10 Mar 2023 14:21:14 +0100 Subject: [PATCH] Correct possible UB in Arc::drop --- src/arc-mutex/arc-drop.md | 3 +-- src/arc-mutex/arc-final.md | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/arc-mutex/arc-drop.md b/src/arc-mutex/arc-drop.md index 3dd9f03..8e1d58c 100644 --- a/src/arc-mutex/arc-drop.md +++ b/src/arc-mutex/arc-drop.md @@ -89,8 +89,7 @@ Now, let's wrap this all up inside the `Drop` implementation: ```rust,ignore impl Drop for Arc { fn drop(&mut self) { - let inner = unsafe { self.ptr.as_ref() }; - if inner.rc.fetch_sub(1, Ordering::Release) != 1 { + if unsafe { self.ptr.as_ref() }.rc.fetch_sub(1, Ordering::Release) != 1 { return; } // This fence is needed to prevent reordering of the use and deletion diff --git a/src/arc-mutex/arc-final.md b/src/arc-mutex/arc-final.md index b9c362d..845774a 100644 --- a/src/arc-mutex/arc-final.md +++ b/src/arc-mutex/arc-final.md @@ -68,8 +68,7 @@ impl Clone for Arc { impl Drop for Arc { fn drop(&mut self) { - let inner = unsafe { self.ptr.as_ref() }; - if inner.rc.fetch_sub(1, Ordering::Release) != 1 { + if unsafe { self.ptr.as_ref() }.rc.fetch_sub(1, Ordering::Release) != 1 { return; } // This fence is needed to prevent reordering of the use and deletion