Skip to content

Commit 381c289

Browse files
committed
test for pointer wrapping ICE
1 parent 7a7b853 commit 381c289

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
use std::ptr;
2+
13
fn 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
}

tests/run-pass/ptr_int_casts.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::mem;
2+
use std::ptr;
23

34
fn 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;

0 commit comments

Comments
 (0)