Skip to content

Commit 9b6e432

Browse files
committed
make another test more robust against random alignment
1 parent db159b8 commit 9b6e432

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

tests/compile-fail/unaligned_pointers/unaligned_ptr_addr_of.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
use std::ptr;
55

66
fn main() {
7-
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
8-
let x = &x[0] as *const _ as *const u32;
9-
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
10-
// The deref is UB even if we just put the result into a raw pointer.
11-
let _x = unsafe { ptr::raw_const!(*x) }; //~ ERROR memory with alignment 2, but alignment 4 is required
7+
for _ in 0..10 { // Try many times as this might work by chance.
8+
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
9+
let x = &x[0] as *const _ as *const u32;
10+
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
11+
// The deref is UB even if we just put the result into a raw pointer.
12+
let _x = unsafe { ptr::raw_const!(*x) }; //~ ERROR memory with alignment 2, but alignment 4 is required
13+
}
1214
}

0 commit comments

Comments
 (0)