Skip to content

Commit

Permalink
Fix the Box::into_inner's double free.
Browse files Browse the repository at this point in the history
  • Loading branch information
wada314 committed Nov 17, 2024
1 parent 367168a commit 5ba2f05
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/stable/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,13 +584,16 @@ impl<T, A: Allocator> Box<T, A> {
/// ```
#[inline(always)]
pub fn into_inner(boxed: Self) -> T {
// Override our default `Drop` implementation.
// Though the default `Drop` implementation drops the both the pointer and the allocator,
// here we only want to drop the allocator.
let boxed = mem::ManuallyDrop::new(boxed);
let alloc = unsafe { ptr::read(&boxed.1) };

let ptr = boxed.0;
let unboxed = unsafe { ptr.as_ptr().read() };
unsafe {
boxed
.1
.deallocate(ptr.as_non_null_ptr().cast(), Layout::new::<T>())
};
unsafe { alloc.deallocate(ptr.as_non_null_ptr().cast(), Layout::new::<T>()) };

unboxed
}
}
Expand Down

0 comments on commit 5ba2f05

Please sign in to comment.