From 562568ba67c0c901508d1b9e359f2e0b992a35ba Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 11 Apr 2020 09:10:43 +0200 Subject: [PATCH 1/2] manually clean up leaked Boxes, to enable Miri's leak checker --- lib.rs | 12 ++++++++++-- scripts/run_miri.sh | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib.rs b/lib.rs index d6fd637..27bfe13 100644 --- a/lib.rs +++ b/lib.rs @@ -2092,12 +2092,17 @@ mod tests { } } + // These boxes are leaked on purpose by panicking `insert_many`, + // so we clean them up manually to appease Miri's leak checker. + let mut box1 = Box::new(false); + let mut box2 = Box::new(false); + let mut vec: SmallVec<[PanicOnDoubleDrop; 0]> = vec![ PanicOnDoubleDrop { - dropped: Box::new(false), + dropped: unsafe { Box::from_raw(&mut *box1) }, }, PanicOnDoubleDrop { - dropped: Box::new(false), + dropped: unsafe { Box::from_raw(&mut *box2) }, }, ] .into(); @@ -2105,6 +2110,9 @@ mod tests { vec.insert_many(0, BadIter); }); assert!(result.is_err()); + + drop(box1); + drop(box2); } #[test] diff --git a/scripts/run_miri.sh b/scripts/run_miri.sh index c5e5376..817928a 100644 --- a/scripts/run_miri.sh +++ b/scripts/run_miri.sh @@ -16,6 +16,6 @@ rustup default "$MIRI_NIGHTLY" rustup component add miri cargo miri setup -cargo miri test --verbose -- -Zmiri-ignore-leaks -cargo miri test --verbose --features union -- -Zmiri-ignore-leaks -cargo miri test --verbose --all-features -- -Zmiri-ignore-leaks +cargo miri test --verbose +cargo miri test --verbose --features union +cargo miri test --verbose --all-features From cbe3620745ea1021c1d0d33809ff150d5aa27471 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 11 Apr 2020 09:11:35 +0200 Subject: [PATCH 2/2] note the leak in insert_many docs --- lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib.rs b/lib.rs index 27bfe13..ed83a75 100644 --- a/lib.rs +++ b/lib.rs @@ -865,6 +865,8 @@ impl SmallVec { /// Insert multiple elements at position `index`, shifting all following elements toward the /// back. + /// + /// Note: when the iterator panics, this can leak memory. pub fn insert_many>(&mut self, index: usize, iterable: I) { let iter = iterable.into_iter(); if index == self.len() {