Skip to content

Commit 3e88291

Browse files
authored
Rollup merge of #136438 - RalfJung:offset_from_ub_errors, r=oli-obk
miri: improve error when offset_from preconditions are violated Fixes #4143
2 parents 9b5d1f1 + dc7a653 commit 3e88291

6 files changed

+45
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
unsafe {
3+
(&1_u8 as *const u8).offset_from(&2_u8); //~ERROR: not both derived from the same allocation
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: `ptr_offset_from` called on two different pointers that are not both derived from the same allocation
2+
--> tests/fail/intrinsics/ptr_offset_from_different_allocs.rs:LL:CC
3+
|
4+
LL | (&1_u8 as *const u8).offset_from(&2_u8);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called on two different pointers that are not both derived from the same 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 tests/fail/intrinsics/ptr_offset_from_different_allocs.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+

tests/fail/intrinsics/ptr_offset_from_different_ints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ fn main() {
1515
let _ = p1.byte_offset_from(p1);
1616

1717
// UB because different pointers.
18-
let _ = p1.byte_offset_from(p2); //~ERROR: no provenance
18+
let _ = p1.byte_offset_from(p2); //~ERROR: not both derived from the same allocation
1919
}
2020
}

tests/fail/intrinsics/ptr_offset_from_different_ints.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: out-of-bounds `offset_from` origin: expected a pointer to the end of 1 byte of memory, but got 0xb[noalloc] which is a dangling pointer (it has no provenance)
1+
error: Undefined Behavior: `ptr_offset_from` called on two different pointers that are not both derived from the same allocation
22
--> tests/fail/intrinsics/ptr_offset_from_different_ints.rs:LL:CC
33
|
44
LL | let _ = p1.byte_offset_from(p2);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds `offset_from` origin: expected a pointer to the end of 1 byte of memory, but got 0xb[noalloc] which is a dangling pointer (it has no provenance)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called on two different pointers that are not both derived from the same allocation
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
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let mem = [0u8; 1];
3+
let ptr = mem.as_ptr();
4+
unsafe {
5+
ptr.wrapping_add(4).offset_from(ptr); //~ERROR: the memory range between them is not in-bounds of an allocation
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: `ptr_offset_from` called on two different pointers where the memory range between them is not in-bounds of an allocation
2+
--> tests/fail/intrinsics/ptr_offset_from_oob.rs:LL:CC
3+
|
4+
LL | ptr.wrapping_add(4).offset_from(ptr);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called on two different pointers where the memory range between them is not in-bounds of an 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 tests/fail/intrinsics/ptr_offset_from_oob.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)