File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed 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-strict-provenance
2+ #![ feature( strict_provenance) ]
3+
4+ use std:: mem;
5+
6+ // This is the example from
7+ // <https://github.com/rust-lang/unsafe-code-guidelines/issues/286#issuecomment-1085144431>.
8+
9+ unsafe fn deref ( left : * const u8 , right : * const u8 ) {
10+ let left_int: usize = mem:: transmute ( left) ; //~ERROR expected initialized plain (non-pointer) bytes
11+ let right_int: usize = mem:: transmute ( right) ;
12+ if left_int == right_int {
13+ // The compiler is allowed to replace `left_int` by `right_int` here...
14+ let left_ptr: * const u8 = mem:: transmute ( left_int) ;
15+ // ...which however means here it could be dereferencing the wrong pointer.
16+ let _val = * left_ptr;
17+ }
18+ }
19+
20+ fn main ( ) {
21+ let ptr1 = & 0u8 as * const u8 ;
22+ let ptr2 = & 1u8 as * const u8 ;
23+ unsafe {
24+ // Two pointers with the same address but different provenance.
25+ deref ( ptr1, ptr2. with_addr ( ptr1. addr ( ) ) ) ;
26+ }
27+ }
You can’t perform that action at this time.
0 commit comments