Skip to content

Commit 7cbb3b1

Browse files
authored
Auto merge of #271 - saethlin:drain-aliasing-test, r=jdm
Test for drains that shift the tail, when inline Previously, the test suite only had one trip through the tail-shifting code in Drain::drop, and that is in the heap state. In the current implementation, a tail-shifting drain while in the inline state produces potentially dangerous aliasing which is currently accepted by default Miri and rejected with -Ztrack-raw-pointers. Adding this test case ensures that if this ever becomes an actual problem it will be easy to find.
2 parents 218e0bb + 0fced9d commit 7cbb3b1

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/tests.rs

+7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ fn drain() {
111111
assert_eq!(v.drain(1..).collect::<Vec<_>>(), &[4, 5]);
112112
// drain should not change the capacity
113113
assert_eq!(v.capacity(), old_capacity);
114+
115+
// Exercise the tail-shifting code when in the inline state
116+
// This has the potential to produce UB due to aliasing
117+
let mut v: SmallVec<[u8; 2]> = SmallVec::new();
118+
v.push(1);
119+
v.push(2);
120+
assert_eq!(v.drain(..1).collect::<Vec<_>>(), &[1]);
114121
}
115122

116123
#[test]

0 commit comments

Comments
 (0)