Skip to content

Commit a163bd1

Browse files
authored
Rollup merge of #124923 - RalfJung:offset-from-errors, r=compiler-errors
interpret/miri: better errors on failing offset_from Fixes #3104
2 parents ea21097 + dd3e16b commit a163bd1

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(strict_provenance)]
2+
use core::ptr;
3+
4+
fn main() {
5+
unsafe {
6+
let base = ptr::without_provenance::<()>(10);
7+
let unit = &*base;
8+
let p1 = unit as *const ();
9+
10+
let base = ptr::without_provenance::<()>(11);
11+
let unit = &*base;
12+
let p2 = unit as *const ();
13+
14+
// Seems to work because they are same pointer
15+
// even though it's dangling.
16+
let _ = p1.byte_offset_from(p1);
17+
18+
// UB because different pointers.
19+
let _ = p1.byte_offset_from(p2); //~ERROR: different pointers without provenance
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: `ptr_offset_from` called on different pointers without provenance (i.e., without an associated allocation)
2+
--> $DIR/ptr_offset_from_different_ints.rs:LL:CC
3+
|
4+
LL | let _ = p1.byte_offset_from(p2);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called on different pointers without provenance (i.e., without an associated allocation)
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/ptr_offset_from_different_ints.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)