diff --git a/tests/run-pass/ptr_arith_offset_overflow.rs b/tests/run-pass/ptr_arith_offset_overflow.rs index 6b778248be5c..56fd448b0cc2 100644 --- a/tests/run-pass/ptr_arith_offset_overflow.rs +++ b/tests/run-pass/ptr_arith_offset_overflow.rs @@ -1,9 +1,12 @@ +use std::ptr; + fn main() { let v = [1i16, 2]; - let x = v.as_ptr().wrapping_offset(1); // ptr to the 2nd element + let x = &mut ptr::null(); // going through memory as there are more sanity checks along that path + *x = v.as_ptr().wrapping_offset(1); // ptr to the 2nd element // Adding 2*isize::max and then 1 is like substracting 1 - let x = x.wrapping_offset(isize::max_value()); - let x = x.wrapping_offset(isize::max_value()); - let x = x.wrapping_offset(1); - assert_eq!(unsafe { *x }, 1); + *x = x.wrapping_offset(isize::max_value()); + *x = x.wrapping_offset(isize::max_value()); + *x = x.wrapping_offset(1); + assert_eq!(unsafe { **x }, 1); } diff --git a/tests/run-pass/ptr_int_casts.rs b/tests/run-pass/ptr_int_casts.rs index b1b06263056d..c279024f35ea 100644 --- a/tests/run-pass/ptr_int_casts.rs +++ b/tests/run-pass/ptr_int_casts.rs @@ -1,4 +1,5 @@ use std::mem; +use std::ptr; fn eq_ref(x: &T, y: &T) -> bool { x as *const _ == y as *const _ @@ -11,6 +12,12 @@ fn main() { assert_eq!(1 as *const i32 as usize, 1); assert_eq!((1 as *const i32).wrapping_offset(4) as usize, 1 + 4*4); + // negative overflowing wrapping_offset (going through memory because + // this used to trigger an ICE on 32bit) + let val = &mut ptr::null(); + *val = (1 as *const u8).wrapping_offset(-4); + assert_eq!(*val as usize, usize::max_value() - 2); + { // ptr-int-ptr let x = 13; let mut y = &x as &_ as *const _ as usize;