-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Audit liballoc for leaks in Drop
impls when user destructor panics
#67290
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a859ca5
Fix `binary_heap::DrainSorted` drop leak on panics
jonas-schievink 3e5eb26
Fix `VecDeque::truncate` leak on drop panic
jonas-schievink 5d04790
Avoid leak in `vec::Drain` when item drop panics
jonas-schievink dc49245
Fix leak in btree_map::IntoIter when drop panics
jonas-schievink b04ca13
Fix leak in VecDeque::drain when drop panics
jonas-schievink 163ed23
Fix leak in vec::IntoIter when a destructor panics
jonas-schievink 0ae16b4
Avoid leak in DrainFilter when a drop panics
jonas-schievink 1f373f4
Add test for panic in LL DrainFilter predicate
jonas-schievink 75f721d
Move VecDeque Drain iterator to new file
jonas-schievink 52d6c90
Update comments in `Drain`s `Drop` impl
jonas-schievink e5987a0
Format
jonas-schievink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use core::iter::FusedIterator; | ||
use core::ptr::{self, NonNull}; | ||
use core::{fmt, mem}; | ||
|
||
use super::{count, Iter, VecDeque}; | ||
|
||
/// A draining iterator over the elements of a `VecDeque`. | ||
/// | ||
/// This `struct` is created by the [`drain`] method on [`VecDeque`]. See its | ||
/// documentation for more. | ||
/// | ||
/// [`drain`]: struct.VecDeque.html#method.drain | ||
/// [`VecDeque`]: struct.VecDeque.html | ||
#[stable(feature = "drain", since = "1.6.0")] | ||
pub struct Drain<'a, T: 'a> { | ||
pub(crate) after_tail: usize, | ||
pub(crate) after_head: usize, | ||
pub(crate) iter: Iter<'a, T>, | ||
pub(crate) deque: NonNull<VecDeque<T>>, | ||
} | ||
|
||
#[stable(feature = "collection_debug", since = "1.17.0")] | ||
impl<T: fmt::Debug> fmt::Debug for Drain<'_, T> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_tuple("Drain") | ||
.field(&self.after_tail) | ||
.field(&self.after_head) | ||
.field(&self.iter) | ||
.finish() | ||
} | ||
} | ||
|
||
#[stable(feature = "drain", since = "1.6.0")] | ||
unsafe impl<T: Sync> Sync for Drain<'_, T> {} | ||
#[stable(feature = "drain", since = "1.6.0")] | ||
unsafe impl<T: Send> Send for Drain<'_, T> {} | ||
|
||
#[stable(feature = "drain", since = "1.6.0")] | ||
impl<T> Drop for Drain<'_, T> { | ||
fn drop(&mut self) { | ||
struct DropGuard<'r, 'a, T>(&'r mut Drain<'a, T>); | ||
|
||
impl<'r, 'a, T> Drop for DropGuard<'r, 'a, T> { | ||
fn drop(&mut self) { | ||
self.0.for_each(drop); | ||
|
||
let source_deque = unsafe { self.0.deque.as_mut() }; | ||
|
||
// T = source_deque_tail; H = source_deque_head; t = drain_tail; h = drain_head | ||
// | ||
// T t h H | ||
// [. . . o o x x o o . . .] | ||
// | ||
let orig_tail = source_deque.tail; | ||
let drain_tail = source_deque.head; | ||
let drain_head = self.0.after_tail; | ||
let orig_head = self.0.after_head; | ||
|
||
let tail_len = count(orig_tail, drain_tail, source_deque.cap()); | ||
let head_len = count(drain_head, orig_head, source_deque.cap()); | ||
|
||
// Restore the original head value | ||
source_deque.head = orig_head; | ||
|
||
match (tail_len, head_len) { | ||
(0, 0) => { | ||
source_deque.head = 0; | ||
source_deque.tail = 0; | ||
} | ||
(0, _) => { | ||
source_deque.tail = drain_head; | ||
} | ||
(_, 0) => { | ||
source_deque.head = drain_tail; | ||
} | ||
_ => unsafe { | ||
if tail_len <= head_len { | ||
source_deque.tail = source_deque.wrap_sub(drain_head, tail_len); | ||
source_deque.wrap_copy(source_deque.tail, orig_tail, tail_len); | ||
} else { | ||
source_deque.head = source_deque.wrap_add(drain_tail, head_len); | ||
source_deque.wrap_copy(drain_tail, drain_head, head_len); | ||
} | ||
}, | ||
} | ||
} | ||
} | ||
|
||
while let Some(item) = self.next() { | ||
let guard = DropGuard(self); | ||
drop(item); | ||
mem::forget(guard); | ||
} | ||
|
||
DropGuard(self); | ||
} | ||
} | ||
|
||
#[stable(feature = "drain", since = "1.6.0")] | ||
impl<T> Iterator for Drain<'_, T> { | ||
type Item = T; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<T> { | ||
self.iter.next().map(|elt| unsafe { ptr::read(elt) }) | ||
} | ||
|
||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.iter.size_hint() | ||
} | ||
} | ||
|
||
#[stable(feature = "drain", since = "1.6.0")] | ||
impl<T> DoubleEndedIterator for Drain<'_, T> { | ||
#[inline] | ||
fn next_back(&mut self) -> Option<T> { | ||
self.iter.next_back().map(|elt| unsafe { ptr::read(elt) }) | ||
} | ||
} | ||
|
||
#[stable(feature = "drain", since = "1.6.0")] | ||
impl<T> ExactSizeIterator for Drain<'_, T> {} | ||
|
||
#[stable(feature = "fused", since = "1.26.0")] | ||
impl<T> FusedIterator for Drain<'_, T> {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the stuff in this
unsafe
block also run when unwinding from user drop? It looks like it does more deallocation.