]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/intptrcast_alignment_check.rs
adjust tests for eager pointer checks on deref
[rust.git] / tests / compile-fail / intptrcast_alignment_check.rs
1 // Even with intptrcast and without validation, we want to be *sure* to catch bugs
2 // that arise from pointers being insufficiently aligned. The only way to achieve
3 // that is not not let programs exploit integer information for alignment, so here
4 // we test that this is indeed the case.
5 fn main() {
6     let x = &mut [0u8; 3];
7     let base_addr = x as *mut _ as usize;
8     let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr+1 };
9     let u16_ptr = base_addr_aligned as *mut u16;
10     unsafe { *u16_ptr = 2; } //~ ERROR tried to access memory with alignment 1, but alignment 2 is required
11     println!("{:?}", x);
12 }