Skip to content

Commit 0cf6061

Browse files
committed
Fix double drop of allocator in IntoIter impl of Vec
1 parent 4ce257f commit 0cf6061

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

library/alloc/src/vec/into_iter.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use core::iter::{
88
FusedIterator, InPlaceIterable, SourceIter, TrustedLen, TrustedRandomAccessNoCoerce,
99
};
1010
use core::marker::PhantomData;
11-
use core::mem::{self};
11+
use core::mem::{self, ManuallyDrop};
12+
use core::ops::Deref;
1213
use core::ptr::{self, NonNull};
1314
use core::slice::{self};
1415

@@ -32,7 +33,9 @@ pub struct IntoIter<
3233
pub(super) buf: NonNull<T>,
3334
pub(super) phantom: PhantomData<T>,
3435
pub(super) cap: usize,
35-
pub(super) alloc: A,
36+
// the drop impl reconstructs a RawVec from buf, cap and alloc
37+
// to avoid dropping the allocator twice we need to wrap it into ManuallyDrop
38+
pub(super) alloc: ManuallyDrop<A>,
3639
pub(super) ptr: *const T,
3740
pub(super) end: *const T,
3841
}
@@ -295,11 +298,11 @@ where
295298
impl<T: Clone, A: Allocator + Clone> Clone for IntoIter<T, A> {
296299
#[cfg(not(test))]
297300
fn clone(&self) -> Self {
298-
self.as_slice().to_vec_in(self.alloc.clone()).into_iter()
301+
self.as_slice().to_vec_in(self.alloc.deref().clone()).into_iter()
299302
}
300303
#[cfg(test)]
301304
fn clone(&self) -> Self {
302-
crate::slice::to_vec(self.as_slice(), self.alloc.clone()).into_iter()
305+
crate::slice::to_vec(self.as_slice(), self.alloc.deref().clone()).into_iter()
303306
}
304307
}
305308

@@ -311,8 +314,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter<T, A> {
311314
impl<T, A: Allocator> Drop for DropGuard<'_, T, A> {
312315
fn drop(&mut self) {
313316
unsafe {
314-
// `IntoIter::alloc` is not used anymore after this
315-
let alloc = ptr::read(&self.0.alloc);
317+
// `IntoIter::alloc` is not used anymore after this and will be dropped by RawVec
318+
let alloc = ManuallyDrop::into_inner(ptr::read(&self.0.alloc));
316319
// RawVec handles deallocation
317320
let _ = RawVec::from_raw_parts_in(self.0.buf.as_ptr(), self.0.cap, alloc);
318321
}

library/alloc/src/vec/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2575,7 +2575,7 @@ impl<T, A: Allocator> IntoIterator for Vec<T, A> {
25752575
fn into_iter(self) -> IntoIter<T, A> {
25762576
unsafe {
25772577
let mut me = ManuallyDrop::new(self);
2578-
let alloc = ptr::read(me.allocator());
2578+
let alloc = ManuallyDrop::new(ptr::read(me.allocator()));
25792579
let begin = me.as_mut_ptr();
25802580
let end = if mem::size_of::<T>() == 0 {
25812581
arith_offset(begin as *const i8, me.len() as isize) as *const T

0 commit comments

Comments
 (0)