Skip to content

Commit

Permalink
fix(cordyceps): add Drop impl for List (#203)
Browse files Browse the repository at this point in the history
When a linked list's `Handle` type owns the allocation for the item in
the list, it is necessary to drain all entries from the list and drop
them when the `List` instance is dropped. Otherwise, the entries may be
leaked.

This fixes a leak reported by Miri.

Fixes #165

BREAKING CHANGE:

The `List::new` constructor now requires a `T: Linked<list::Links<T>>`
bound.
  • Loading branch information
hawkw committed Jun 7, 2022
1 parent 739676d commit 62841c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
17 changes: 13 additions & 4 deletions cordyceps/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ mod tests;
///
/// [intrusive]: crate#intrusive-data-structures
/// [`list::Links<T>`]: crate::list::Links
pub struct List<T: ?Sized> {
pub struct List<T: Linked<Links<T>> + ?Sized> {
head: Link<T>,
tail: Link<T>,
len: usize,
Expand Down Expand Up @@ -248,7 +248,8 @@ struct LinksInner<T: ?Sized> {
}

// ==== impl List ====
impl<T: ?Sized> List<T> {

impl<T: Linked<Links<T>> + ?Sized> List<T> {
/// Returns a new empty list.
#[must_use]
pub const fn new() -> List<T> {
Expand Down Expand Up @@ -286,9 +287,7 @@ impl<T: ?Sized> List<T> {
pub fn len(&self) -> usize {
self.len
}
}

impl<T: Linked<Links<T>> + ?Sized> List<T> {
/// Asserts as many of the linked list's invariants as possible.
pub fn assert_valid(&self) {
let head = match self.head {
Expand Down Expand Up @@ -513,6 +512,16 @@ impl<T: Linked<Links<T>> + ?Sized> fmt::Debug for List<T> {
}
}

impl<T: Linked<Links<T>> + ?Sized> Drop for List<T> {
fn drop(&mut self) {
while let Some(node) = self.pop_front() {
drop(node);
}

debug_assert!(self.is_empty());
}
}

// ==== impl Links ====

impl<T: ?Sized> Links<T> {
Expand Down
3 changes: 1 addition & 2 deletions cordyceps/src/list/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,10 @@ proptest::proptest! {
fn run_fuzz(ops: Vec<Op>) {
use std::collections::VecDeque;

let entries: Vec<_> = (0..ops.len()).map(|i| entry(i as i32)).collect();
let mut ll = List::<Entry<'_>>::new();
let mut reference = VecDeque::new();

let entries: Vec<_> = (0..ops.len()).map(|i| entry(i as i32)).collect();

for (i, op) in ops.iter().enumerate() {
let _span = tracing::info_span!("op", ?i, ?op).entered();
tracing::info!(?op);
Expand Down

0 comments on commit 62841c0

Please sign in to comment.