Skip to content

Commit d52989a

Browse files
committed
Add failing leak detection test
1 parent 5142ad5 commit d52989a

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

crates/slice-dst/tests/leak.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use {
2+
slice_dst::*,
3+
std::{
4+
panic,
5+
sync::atomic::{AtomicUsize, Ordering::SeqCst},
6+
},
7+
};
8+
9+
struct DropTracking<'a> {
10+
place: &'a AtomicUsize,
11+
}
12+
13+
impl<'a> DropTracking<'a> {
14+
fn new(place: &'a AtomicUsize) -> Self {
15+
place.fetch_add(1, SeqCst);
16+
DropTracking { place }
17+
}
18+
}
19+
20+
impl Drop for DropTracking<'_> {
21+
fn drop(&mut self) {
22+
self.place.fetch_sub(1, SeqCst);
23+
}
24+
}
25+
26+
#[test]
27+
#[cfg_attr(
28+
all(miri, target_os = "windows"),
29+
ignore = "miri does not support panicking on windows rust-lang/miri#1059"
30+
)]
31+
fn bad_exactsizeiterator() {
32+
struct Iter<'a> {
33+
counter: &'a AtomicUsize,
34+
len: usize,
35+
}
36+
37+
impl ExactSizeIterator for Iter<'_> {
38+
fn len(&self) -> usize {
39+
self.len
40+
}
41+
}
42+
43+
impl<'a> Iterator for Iter<'a> {
44+
type Item = DropTracking<'a>;
45+
46+
fn next(&mut self) -> Option<Self::Item> {
47+
match self.len {
48+
0 | 1 => None,
49+
_ => {
50+
self.len -= 1;
51+
Some(DropTracking::new(self.counter))
52+
}
53+
}
54+
}
55+
}
56+
57+
let mut counter = AtomicUsize::new(0);
58+
let _ = std::panic::catch_unwind(|| {
59+
let _: Box<_> = SliceWithHeader::new::<Box<_>, _>(
60+
(),
61+
Iter {
62+
counter: &counter,
63+
len: 5,
64+
},
65+
);
66+
});
67+
assert_eq!(*counter.get_mut(), 0);
68+
}

0 commit comments

Comments
 (0)