Skip to content

Commit 080d413

Browse files
authored
Rollup merge of #69828 - RalfJung:vec-leak, r=kennytm
fix memory leak when vec::IntoIter panics during drop Fixes #69770
2 parents 9674c09 + 528cbc4 commit 080d413

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/liballoc/vec.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -2626,13 +2626,21 @@ impl<T: Clone> Clone for IntoIter<T> {
26262626
#[stable(feature = "rust1", since = "1.0.0")]
26272627
unsafe impl<#[may_dangle] T> Drop for IntoIter<T> {
26282628
fn drop(&mut self) {
2629+
struct DropGuard<'a, T>(&'a mut IntoIter<T>);
2630+
2631+
impl<T> Drop for DropGuard<'_, T> {
2632+
fn drop(&mut self) {
2633+
// RawVec handles deallocation
2634+
let _ = unsafe { RawVec::from_raw_parts(self.0.buf.as_ptr(), self.0.cap) };
2635+
}
2636+
}
2637+
2638+
let guard = DropGuard(self);
26292639
// destroy the remaining elements
26302640
unsafe {
2631-
ptr::drop_in_place(self.as_mut_slice());
2641+
ptr::drop_in_place(guard.0.as_mut_slice());
26322642
}
2633-
2634-
// RawVec handles deallocation
2635-
let _ = unsafe { RawVec::from_raw_parts(self.buf.as_ptr(), self.cap) };
2643+
// now `guard` will be dropped and do the rest
26362644
}
26372645
}
26382646

0 commit comments

Comments
 (0)