File tree Expand file tree Collapse file tree 2 files changed +15
-5
lines changed Expand file tree Collapse file tree 2 files changed +15
-5
lines changed Original file line number Diff line number Diff line change 1+ use std:: ptr;
2+
13fn main ( ) {
24 let v = [ 1i16 , 2 ] ;
3- let x = v. as_ptr ( ) . wrapping_offset ( 1 ) ; // ptr to the 2nd element
5+ let x = & mut ptr:: null ( ) ; // going through memory as there are more sanity checks along that path
6+ * x = v. as_ptr ( ) . wrapping_offset ( 1 ) ; // ptr to the 2nd element
47 // Adding 2*isize::max and then 1 is like substracting 1
5- let x = x. wrapping_offset ( isize:: max_value ( ) ) ;
6- let x = x. wrapping_offset ( isize:: max_value ( ) ) ;
7- let x = x. wrapping_offset ( 1 ) ;
8- assert_eq ! ( unsafe { * x } , 1 ) ;
8+ * x = x. wrapping_offset ( isize:: max_value ( ) ) ;
9+ * x = x. wrapping_offset ( isize:: max_value ( ) ) ;
10+ * x = x. wrapping_offset ( 1 ) ;
11+ assert_eq ! ( unsafe { * * x } , 1 ) ;
912}
Original file line number Diff line number Diff line change 11use std:: mem;
2+ use std:: ptr;
23
34fn eq_ref < T > ( x : & T , y : & T ) -> bool {
45 x as * const _ == y as * const _
@@ -11,6 +12,12 @@ fn main() {
1112 assert_eq ! ( 1 as * const i32 as usize , 1 ) ;
1213 assert_eq ! ( ( 1 as * const i32 ) . wrapping_offset( 4 ) as usize , 1 + 4 * 4 ) ;
1314
15+ // negative overflowing wrapping_offset (going through memory because
16+ // this used to trigger an ICE on 32bit)
17+ let val = & mut ptr:: null ( ) ;
18+ * val = ( 1 as * const u8 ) . wrapping_offset ( -4 ) ;
19+ assert_eq ! ( * val as usize , usize :: max_value( ) - 2 ) ;
20+
1421 { // ptr-int-ptr
1522 let x = 13 ;
1623 let mut y = & x as & _ as * const _ as usize ;
You can’t perform that action at this time.
0 commit comments