File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
src/tools/miri/tests/fail/unaligned_pointers Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ //@ compile-flags: -Zmiri-symbolic-alignment-check
2+ //@ run-pass
3+
4+ use std:: ptr;
5+
6+ // This is a ZST (Zero-Sized Type) that requires high alignment (alignment 8).
7+ // The original bug occurred because IntoIter::nth_back tried to calculate an
8+ // unaligned pointer for drop_in_place based on this high-aligned ZST, leading to UB in Miri.
9+ #[ repr( align( 8 ) ) ]
10+ struct Thing ;
11+
12+ fn main ( ) {
13+ // Create a Vec containing two high-aligned ZSTs.
14+ let v = vec ! [ Thing , Thing ] ;
15+
16+ // Calling IntoIter::nth_back(1) triggers the problematic drop_in_place logic
17+ // within Vec::IntoIter. The fix (using ptr::NonNull::dangling()) prevents the UB.
18+ let mut iter = v. into_iter ( ) ;
19+ let _ = iter. nth_back ( 1 ) ;
20+
21+ // Also test nth_back(0) for completeness.
22+ let v2 = vec ! [ Thing , Thing ] ;
23+ let mut iter2 = v2. into_iter ( ) ;
24+ let _ = iter2. nth_back ( 0 ) ;
25+
26+ // The main assertion is that the program runs to completion without Miri reporting UB.
27+ }
You can’t perform that action at this time.
0 commit comments