Skip to content

Commit 3a5a47b

Browse files
Rollup merge of #130239 - RalfJung:miri-ptr-offset-unsigned, r=compiler-errors
miri: fix overflow detection for unsigned pointer offset This is the Miri part of rust-lang/rust#130229. This is already UB in codegen so we better make Miri detect it; updating the docs may take time if we have to follow some approval process, but let's make Miri match reality ASAP. r? ``@scottmcm``
2 parents 424a301 + 9349fce commit 3a5a47b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let x = &[0i32; 2];
3+
let x = x.as_ptr().wrapping_add(1);
4+
// If the `!0` is interpreted as `isize`, it is just `-1` and hence harmless.
5+
// However, this is unsigned arithmetic, so really this is `usize::MAX` and hence UB.
6+
unsafe { x.byte_add(!0).read() }; //~ERROR: does not fit in an `isize`
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: overflowing pointer arithmetic: the total offset in bytes does not fit in an `isize`
2+
--> $DIR/ptr_offset_unsigned_overflow.rs:LL:CC
3+
|
4+
LL | unsafe { x.byte_add(!0).read() };
5+
| ^^^^^^^^^^^^^^ overflowing pointer arithmetic: the total offset in bytes does not fit in an `isize`
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_unsigned_overflow.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)