Skip to content

Commit

Permalink
Test for drains that shift the tail, when inline
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
saethlin committed Jan 3, 2022
1 parent 218e0bb commit 0fced9d
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ fn drain() {
assert_eq!(v.drain(1..).collect::<Vec<_>>(), &[4, 5]);
// drain should not change the capacity
assert_eq!(v.capacity(), old_capacity);

// Exercise the tail-shifting code when in the inline state
// This has the potential to produce UB due to aliasing
let mut v: SmallVec<[u8; 2]> = SmallVec::new();
v.push(1);
v.push(2);
assert_eq!(v.drain(..1).collect::<Vec<_>>(), &[1]);
}

#[test]
Expand Down

0 comments on commit 0fced9d

Please sign in to comment.