test for pointer wrapping ICE

This commit is contained in:
Ralf Jung 2019-05-26 14:47:37 +02:00
parent 7a7b853120
commit 381c2897b0
2 changed files with 15 additions and 5 deletions

View file

@ -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);
}

View file

@ -1,4 +1,5 @@
use std::mem;
use std::ptr;
fn eq_ref<T>(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;