Skip to content

Commit 35252eb

Browse files
committed
Auto merge of #2438 - RalfJung:more-track-caller, r=RalfJung
adjust for more backtrace pruning The Miri side of rust-lang/rust#99690. Those messages are much nicer. :) And we also need error-pattern much less.
2 parents 6227e1e + 982979e commit 35252eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+110
-205
lines changed

rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7fe022f5aa32bbbb33c3a58755729d6667a461a9
1+
2fdbf075cf502431ca9fee6616331b32e34f25de
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//@error-pattern: overflow computing total size
21
use std::mem;
32

43
fn main() {
54
let x = 0;
65
let mut y = 0;
76
unsafe {
87
(&mut y as *mut i32).copy_from(&x, 1usize << (mem::size_of::<usize>() * 8 - 1));
8+
//~^ERROR: overflow computing total size
99
}
1010
}

tests/fail/intrinsics/copy_overflow.stderr

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
error: Undefined Behavior: overflow computing total size of `copy`
2-
--> RUSTLIB/core/src/intrinsics.rs:LL:CC
2+
--> $DIR/copy_overflow.rs:LL:CC
33
|
4-
LL | copy(src, dst, count)
5-
| ^^^^^^^^^^^^^^^^^^^^^ overflow computing total size of `copy`
4+
LL | (&mut y as *mut i32).copy_from(&x, 1usize << (mem::size_of::<usize>() * 8 - 1));
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overflow computing total size of `copy`
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
= note: backtrace:
10-
= note: inside `std::intrinsics::copy::<i32>` at RUSTLIB/core/src/intrinsics.rs:LL:CC
11-
= note: inside `std::ptr::mut_ptr::<impl *mut i32>::copy_from` at RUSTLIB/core/src/ptr/mut_ptr.rs:LL:CC
12-
note: inside `main` at $DIR/copy_overflow.rs:LL:CC
13-
--> $DIR/copy_overflow.rs:LL:CC
14-
|
15-
LL | (&mut y as *mut i32).copy_from(&x, 1usize << (mem::size_of::<usize>() * 8 - 1));
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
= note: inside `main` at $DIR/copy_overflow.rs:LL:CC
1711

1812
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1913

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
//@error-pattern: pointer to 5 bytes starting at offset 0 is out-of-bounds
21
fn main() {
32
let v = [0i8; 4];
43
let x = &v as *const i8;
54
// The error is inside another function, so we cannot match it by line
6-
let x = unsafe { x.offset(5) };
5+
let x = unsafe { x.offset(5) }; //~ERROR: pointer to 5 bytes starting at offset 0 is out-of-bounds
76
panic!("this should never print: {:?}", x);
87
}

tests/fail/intrinsics/out_of_bounds_ptr_1.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
error: Undefined Behavior: out-of-bounds pointer arithmetic: ALLOC has size 4, so pointer to 5 bytes starting at offset 0 is out-of-bounds
2-
--> RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
2+
--> $DIR/out_of_bounds_ptr_1.rs:LL:CC
33
|
4-
LL | unsafe { intrinsics::offset(self, count) }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer arithmetic: ALLOC has size 4, so pointer to 5 bytes starting at offset 0 is out-of-bounds
4+
LL | let x = unsafe { x.offset(5) };
5+
| ^^^^^^^^^^^ out-of-bounds pointer arithmetic: ALLOC has size 4, so pointer to 5 bytes starting at offset 0 is out-of-bounds
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
= note: backtrace:
10-
= note: inside `std::ptr::const_ptr::<impl *const i8>::offset` at RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
11-
note: inside `main` at $DIR/out_of_bounds_ptr_1.rs:LL:CC
12-
--> $DIR/out_of_bounds_ptr_1.rs:LL:CC
13-
|
14-
LL | let x = unsafe { x.offset(5) };
15-
| ^^^^^^^^^^^
10+
= note: inside `main` at $DIR/out_of_bounds_ptr_1.rs:LL:CC
1611

1712
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1813

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
//@error-pattern: overflowing in-bounds pointer arithmetic
21
fn main() {
32
let v = [0i8; 4];
43
let x = &v as *const i8;
5-
let x = unsafe { x.offset(isize::MIN) };
4+
let x = unsafe { x.offset(isize::MIN) }; //~ERROR: overflowing in-bounds pointer arithmetic
65
panic!("this should never print: {:?}", x);
76
}

tests/fail/intrinsics/out_of_bounds_ptr_2.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
error: Undefined Behavior: overflowing in-bounds pointer arithmetic
2-
--> RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
2+
--> $DIR/out_of_bounds_ptr_2.rs:LL:CC
33
|
4-
LL | unsafe { intrinsics::offset(self, count) }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overflowing in-bounds pointer arithmetic
4+
LL | let x = unsafe { x.offset(isize::MIN) };
5+
| ^^^^^^^^^^^^^^^^^^^^ overflowing in-bounds pointer arithmetic
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
= note: backtrace:
10-
= note: inside `std::ptr::const_ptr::<impl *const i8>::offset` at RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
11-
note: inside `main` at $DIR/out_of_bounds_ptr_2.rs:LL:CC
12-
--> $DIR/out_of_bounds_ptr_2.rs:LL:CC
13-
|
14-
LL | let x = unsafe { x.offset(isize::MIN) };
15-
| ^^^^^^^^^^^^^^^^^^^^
10+
= note: inside `main` at $DIR/out_of_bounds_ptr_2.rs:LL:CC
1611

1712
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1813

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
//@error-pattern: pointer to 1 byte starting at offset -1 is out-of-bounds
21
fn main() {
32
let v = [0i8; 4];
43
let x = &v as *const i8;
5-
let x = unsafe { x.offset(-1) };
4+
let x = unsafe { x.offset(-1) }; //~ERROR: pointer to 1 byte starting at offset -1 is out-of-bounds
65
panic!("this should never print: {:?}", x);
76
}

tests/fail/intrinsics/out_of_bounds_ptr_3.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
error: Undefined Behavior: out-of-bounds pointer arithmetic: ALLOC has size 4, so pointer to 1 byte starting at offset -1 is out-of-bounds
2-
--> RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
2+
--> $DIR/out_of_bounds_ptr_3.rs:LL:CC
33
|
4-
LL | unsafe { intrinsics::offset(self, count) }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer arithmetic: ALLOC has size 4, so pointer to 1 byte starting at offset -1 is out-of-bounds
4+
LL | let x = unsafe { x.offset(-1) };
5+
| ^^^^^^^^^^^^ out-of-bounds pointer arithmetic: ALLOC has size 4, so pointer to 1 byte starting at offset -1 is out-of-bounds
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
= note: backtrace:
10-
= note: inside `std::ptr::const_ptr::<impl *const i8>::offset` at RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
11-
note: inside `main` at $DIR/out_of_bounds_ptr_3.rs:LL:CC
12-
--> $DIR/out_of_bounds_ptr_3.rs:LL:CC
13-
|
14-
LL | let x = unsafe { x.offset(-1) };
15-
| ^^^^^^^^^^^^
10+
= note: inside `main` at $DIR/out_of_bounds_ptr_3.rs:LL:CC
1611

1712
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1813

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
#![feature(core_intrinsics)]
2-
3-
use std::intrinsics::*;
1+
#![feature(unchecked_math)]
42

53
fn main() {
64
unsafe {
7-
let _n = unchecked_shr(1i64, 64);
5+
let _n = 1i64.unchecked_shr(64);
86
//~^ ERROR: overflowing shift by 64 in `unchecked_shr`
97
}
108
}

tests/fail/intrinsics/overflowing-unchecked-rsh.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: overflowing shift by 64 in `unchecked_shr`
22
--> $DIR/overflowing-unchecked-rsh.rs:LL:CC
33
|
4-
LL | let _n = unchecked_shr(1i64, 64);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^ overflowing shift by 64 in `unchecked_shr`
4+
LL | let _n = 1i64.unchecked_shr(64);
5+
| ^^^^^^^^^^^^^^^^^^^^^^ overflowing shift by 64 in `unchecked_shr`
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
//@error-pattern: null pointer is a dangling pointer
21
//@compile-flags: -Zmiri-permissive-provenance
32

3+
#[rustfmt::skip] // fails with "left behind trailing whitespace"
44
fn main() {
55
let x = 0 as *mut i32;
66
let _x = x.wrapping_offset(8); // ok, this has no inbounds tag
77
let _x = unsafe { x.offset(0) }; // UB despite offset 0, NULL is never inbounds
8+
//~^ERROR: null pointer is a dangling pointer
89
}

tests/fail/intrinsics/ptr_offset_0_plus_0.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
error: Undefined Behavior: out-of-bounds pointer arithmetic: null pointer is a dangling pointer (it has no provenance)
2-
--> RUSTLIB/core/src/ptr/mut_ptr.rs:LL:CC
2+
--> $DIR/ptr_offset_0_plus_0.rs:LL:CC
33
|
4-
LL | unsafe { intrinsics::offset(self, count) as *mut T }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer arithmetic: null pointer is a dangling pointer (it has no provenance)
4+
LL | let _x = unsafe { x.offset(0) }; // UB despite offset 0, NULL is never inbounds
5+
| ^^^^^^^^^^^ out-of-bounds pointer arithmetic: null pointer is a dangling pointer (it has no provenance)
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
= note: backtrace:
10-
= note: inside `std::ptr::mut_ptr::<impl *mut i32>::offset` at RUSTLIB/core/src/ptr/mut_ptr.rs:LL:CC
11-
note: inside `main` at $DIR/ptr_offset_0_plus_0.rs:LL:CC
12-
--> $DIR/ptr_offset_0_plus_0.rs:LL:CC
13-
|
14-
LL | let _x = unsafe { x.offset(0) }; // UB despite offset 0, NULL is never inbounds
15-
| ^^^^^^^^^^^
10+
= note: inside `main` at $DIR/ptr_offset_0_plus_0.rs:LL:CC
1611

1712
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1813

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
#![feature(core_intrinsics)]
2-
3-
use std::intrinsics::ptr_offset_from;
4-
51
fn main() {
62
let start_ptr = &4 as *const _ as *const u8;
73
let length = 10;
84
let end_ptr = start_ptr.wrapping_add(length);
95
// Even if the offset is 0, a dangling OOB pointer is not allowed.
10-
unsafe { ptr_offset_from(end_ptr, end_ptr) }; //~ERROR: pointer at offset 10 is out-of-bounds
6+
unsafe { end_ptr.offset_from(end_ptr) }; //~ERROR: pointer at offset 10 is out-of-bounds
117
}

tests/fail/intrinsics/ptr_offset_from_oob.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: out-of-bounds offset_from: ALLOC has size 4, so pointer at offset 10 is out-of-bounds
22
--> $DIR/ptr_offset_from_oob.rs:LL:CC
33
|
4-
LL | unsafe { ptr_offset_from(end_ptr, end_ptr) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds offset_from: ALLOC has size 4, so pointer at offset 10 is out-of-bounds
4+
LL | unsafe { end_ptr.offset_from(end_ptr) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds offset_from: ALLOC has size 4, so pointer at offset 10 is out-of-bounds
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
//@error-pattern: first pointer has smaller offset than second: 0 < 4
1+
// Preparing for a rustc behavior change that'll happen soon: (FIXME remove this after the next submodule bump succeeded)
2+
//@normalize-stderr-test: "`(ptr_offset_from_unsigned)`" -> "$1"
23
#![feature(ptr_sub_ptr)]
34

45
fn main() {
56
let arr = [0u8; 8];
67
let ptr1 = arr.as_ptr();
78
let ptr2 = ptr1.wrapping_add(4);
8-
let _val = unsafe { ptr1.sub_ptr(ptr2) };
9+
let _val = unsafe { ptr1.sub_ptr(ptr2) }; //~ERROR: first pointer has smaller offset than second: 0 < 4
910
}

tests/fail/intrinsics/ptr_offset_from_unsigned_neg.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
error: Undefined Behavior: ptr_offset_from_unsigned called when first pointer has smaller offset than second: 0 < 4
2-
--> RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
2+
--> $DIR/ptr_offset_from_unsigned_neg.rs:LL:CC
33
|
4-
LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ptr_offset_from_unsigned called when first pointer has smaller offset than second: 0 < 4
4+
LL | let _val = unsafe { ptr1.sub_ptr(ptr2) };
5+
| ^^^^^^^^^^^^^^^^^^ ptr_offset_from_unsigned called when first pointer has smaller offset than second: 0 < 4
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
= note: backtrace:
10-
= note: inside `std::ptr::const_ptr::<impl *const u8>::sub_ptr` at RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
11-
note: inside `main` at $DIR/ptr_offset_from_unsigned_neg.rs:LL:CC
12-
--> $DIR/ptr_offset_from_unsigned_neg.rs:LL:CC
13-
|
14-
LL | let _val = unsafe { ptr1.sub_ptr(ptr2) };
15-
| ^^^^^^^^^^^^^^^^^^
10+
= note: inside `main` at $DIR/ptr_offset_from_unsigned_neg.rs:LL:CC
1611

1712
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1813

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
//@error-pattern: is a dangling pointer
21
//@compile-flags: -Zmiri-permissive-provenance
32

43
fn main() {
54
// Can't offset an integer pointer by non-zero offset.
65
unsafe {
7-
let _val = (1 as *mut u8).offset(1);
6+
let _val = (1 as *mut u8).offset(1); //~ERROR: is a dangling pointer
87
}
98
}

tests/fail/intrinsics/ptr_offset_int_plus_int.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
error: Undefined Behavior: out-of-bounds pointer arithmetic: 0x1[noalloc] is a dangling pointer (it has no provenance)
2-
--> RUSTLIB/core/src/ptr/mut_ptr.rs:LL:CC
2+
--> $DIR/ptr_offset_int_plus_int.rs:LL:CC
33
|
4-
LL | unsafe { intrinsics::offset(self, count) as *mut T }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer arithmetic: 0x1[noalloc] is a dangling pointer (it has no provenance)
4+
LL | let _val = (1 as *mut u8).offset(1);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer arithmetic: 0x1[noalloc] is a dangling pointer (it has no provenance)
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
= note: backtrace:
10-
= note: inside `std::ptr::mut_ptr::<impl *mut u8>::offset` at RUSTLIB/core/src/ptr/mut_ptr.rs:LL:CC
11-
note: inside `main` at $DIR/ptr_offset_int_plus_int.rs:LL:CC
12-
--> $DIR/ptr_offset_int_plus_int.rs:LL:CC
13-
|
14-
LL | let _val = (1 as *mut u8).offset(1);
15-
| ^^^^^^^^^^^^^^^^^^^^^^^^
10+
= note: inside `main` at $DIR/ptr_offset_int_plus_int.rs:LL:CC
1611

1712
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1813

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
//@error-pattern: is a dangling pointer
21
//@compile-flags: -Zmiri-permissive-provenance
32

43
fn main() {
54
let ptr = Box::into_raw(Box::new(0u32));
65
// Can't start with an integer pointer and get to something usable
76
unsafe {
8-
let _val = (1 as *mut u8).offset(ptr as isize);
7+
let _val = (1 as *mut u8).offset(ptr as isize); //~ERROR: is a dangling pointer
98
}
109
}

tests/fail/intrinsics/ptr_offset_int_plus_ptr.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
error: Undefined Behavior: out-of-bounds pointer arithmetic: 0x1[noalloc] is a dangling pointer (it has no provenance)
2-
--> RUSTLIB/core/src/ptr/mut_ptr.rs:LL:CC
2+
--> $DIR/ptr_offset_int_plus_ptr.rs:LL:CC
33
|
4-
LL | unsafe { intrinsics::offset(self, count) as *mut T }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer arithmetic: 0x1[noalloc] is a dangling pointer (it has no provenance)
4+
LL | let _val = (1 as *mut u8).offset(ptr as isize);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer arithmetic: 0x1[noalloc] is a dangling pointer (it has no provenance)
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
= note: backtrace:
10-
= note: inside `std::ptr::mut_ptr::<impl *mut u8>::offset` at RUSTLIB/core/src/ptr/mut_ptr.rs:LL:CC
11-
note: inside `main` at $DIR/ptr_offset_int_plus_ptr.rs:LL:CC
12-
--> $DIR/ptr_offset_int_plus_ptr.rs:LL:CC
13-
|
14-
LL | let _val = (1 as *mut u8).offset(ptr as isize);
15-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
= note: inside `main` at $DIR/ptr_offset_int_plus_ptr.rs:LL:CC
1611

1712
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1813

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
//@error-pattern: overflowing in-bounds pointer arithmetic
21
fn main() {
32
let v = [1i8, 2];
43
let x = &v[1] as *const i8;
5-
let _val = unsafe { x.offset(isize::MIN) };
4+
let _val = unsafe { x.offset(isize::MIN) }; //~ERROR: overflowing in-bounds pointer arithmetic
65
}

tests/fail/intrinsics/ptr_offset_overflow.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
error: Undefined Behavior: overflowing in-bounds pointer arithmetic
2-
--> RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
2+
--> $DIR/ptr_offset_overflow.rs:LL:CC
33
|
4-
LL | unsafe { intrinsics::offset(self, count) }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overflowing in-bounds pointer arithmetic
4+
LL | let _val = unsafe { x.offset(isize::MIN) };
5+
| ^^^^^^^^^^^^^^^^^^^^ overflowing in-bounds pointer arithmetic
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
= note: backtrace:
10-
= note: inside `std::ptr::const_ptr::<impl *const i8>::offset` at RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
11-
note: inside `main` at $DIR/ptr_offset_overflow.rs:LL:CC
12-
--> $DIR/ptr_offset_overflow.rs:LL:CC
13-
|
14-
LL | let _val = unsafe { x.offset(isize::MIN) };
15-
| ^^^^^^^^^^^^^^^^^^^^
10+
= note: inside `main` at $DIR/ptr_offset_overflow.rs:LL:CC
1611

1712
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1813

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//@error-pattern: pointer at offset 32 is out-of-bounds
2-
1+
#[rustfmt::skip] // fails with "left behind trailing whitespace"
32
fn main() {
43
let x = Box::into_raw(Box::new(0u32));
54
let x = x.wrapping_offset(8); // ok, this has no inbounds tag
65
let _x = unsafe { x.offset(0) }; // UB despite offset 0, the pointer is not inbounds of the only object it can point to
6+
//~^ERROR: pointer at offset 32 is out-of-bounds
77
}

tests/fail/intrinsics/ptr_offset_ptr_plus_0.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
error: Undefined Behavior: out-of-bounds pointer arithmetic: ALLOC has size 4, so pointer at offset 32 is out-of-bounds
2-
--> RUSTLIB/core/src/ptr/mut_ptr.rs:LL:CC
2+
--> $DIR/ptr_offset_ptr_plus_0.rs:LL:CC
33
|
4-
LL | unsafe { intrinsics::offset(self, count) as *mut T }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer arithmetic: ALLOC has size 4, so pointer at offset 32 is out-of-bounds
4+
LL | let _x = unsafe { x.offset(0) }; // UB despite offset 0, the pointer is not inbounds of the only object it can point to
5+
| ^^^^^^^^^^^ out-of-bounds pointer arithmetic: ALLOC has size 4, so pointer at offset 32 is out-of-bounds
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
= note: backtrace:
10-
= note: inside `std::ptr::mut_ptr::<impl *mut u32>::offset` at RUSTLIB/core/src/ptr/mut_ptr.rs:LL:CC
11-
note: inside `main` at $DIR/ptr_offset_ptr_plus_0.rs:LL:CC
12-
--> $DIR/ptr_offset_ptr_plus_0.rs:LL:CC
13-
|
14-
LL | let _x = unsafe { x.offset(0) }; // UB despite offset 0, the pointer is not inbounds of the only object it can point to
15-
| ^^^^^^^^^^^
10+
= note: inside `main` at $DIR/ptr_offset_ptr_plus_0.rs:LL:CC
1611

1712
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1813

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#![feature(core_intrinsics)]
1+
#![feature(unchecked_math)]
2+
23
fn main() {
34
// MAX overflow
4-
unsafe {
5-
std::intrinsics::unchecked_add(40000u16, 30000); //~ ERROR: overflow executing `unchecked_add`
6-
}
5+
let _val = unsafe { 40000u16.unchecked_add(30000) }; //~ ERROR: overflow executing `unchecked_add`
76
}

0 commit comments

Comments
 (0)