Skip to content

Commit e68100a

Browse files
committed
Auto merge of #3896 - RalfJung:ptr_offset_unsigned_overflow, r=RalfJung
ptr_offset_unsigned_overflow: extend test Test that indeed, the signed version works before the unsigned version is UB.
2 parents bdab169 + c64b664 commit e68100a

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
fn main() {
22
let x = &[0i32; 2];
33
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`
4+
// If `usize::MAX` is interpreted as `isize`, it is just `-1` and hence harmless.
5+
let _ = unsafe { x.byte_offset(usize::MAX as isize) };
6+
// However, `byte_add` uses unsigned arithmetic, so really this is `usize::MAX` and hence UB.
7+
let _ = unsafe { x.byte_add(usize::MAX) }; //~ERROR: does not fit in an `isize`
78
}

tests/fail/intrinsics/ptr_offset_unsigned_overflow.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: overflowing pointer arithmetic: the total offset in bytes does not fit in an `isize`
22
--> tests/fail/intrinsics/ptr_offset_unsigned_overflow.rs:LL:CC
33
|
4-
LL | unsafe { x.byte_add(!0).read() };
5-
| ^^^^^^^^^^^^^^ overflowing pointer arithmetic: the total offset in bytes does not fit in an `isize`
4+
LL | let _ = unsafe { x.byte_add(usize::MAX) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^ overflowing pointer arithmetic: the total offset in bytes does not fit in an `isize`
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

0 commit comments

Comments
 (0)