File tree 2 files changed +15
-5
lines changed
2 files changed +15
-5
lines changed Original file line number Diff line number Diff line change
1
+ use std:: ptr;
2
+
1
3
fn main ( ) {
2
4
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
4
7
// 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 ) ;
9
12
}
Original file line number Diff line number Diff line change 1
1
use std:: mem;
2
+ use std:: ptr;
2
3
3
4
fn eq_ref < T > ( x : & T , y : & T ) -> bool {
4
5
x as * const _ == y as * const _
@@ -11,6 +12,12 @@ fn main() {
11
12
assert_eq ! ( 1 as * const i32 as usize , 1 ) ;
12
13
assert_eq ! ( ( 1 as * const i32 ) . wrapping_offset( 4 ) as usize , 1 + 4 * 4 ) ;
13
14
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
+
14
21
{ // ptr-int-ptr
15
22
let x = 13 ;
16
23
let mut y = & x as & _ as * const _ as usize ;
You can’t perform that action at this time.
0 commit comments