diff options
| author | Joe Richey <joerichey@google.com> | 2020-05-26 02:00:02 -0700 |
|---|---|---|
| committer | Joe Richey <joerichey@google.com> | 2020-05-26 02:06:18 -0700 |
| commit | 71ef8414bd86cbd79b29f8b1a0145da96e2f2f09 (patch) | |
| tree | 2b38328d2118540ee6bef35088a29382b4ea985c /src/test | |
| parent | 6367b544b781889abee296d34d2b7d353a6ae0f8 (diff) | |
Add checks and tests for computing abs(offset_bytes)
The previous code paniced if offset_bytes == i64::MIN. This commit: - Properly computes the absoulte value to avoid this panic - Adds a test for this edge case Signed-off-by: Joe Richey <joerichey@google.com>
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/ui/consts/offset_ub.rs | 3 | ||||
| -rw-r--r-- | src/test/ui/consts/offset_ub.stderr | 17 |
2 files changed, 19 insertions, 1 deletions
diff --git a/src/test/ui/consts/offset_ub.rs b/src/test/ui/consts/offset_ub.rs index ac120789694..4f943ed9ad1 100644 --- a/src/test/ui/consts/offset_ub.rs +++ b/src/test/ui/consts/offset_ub.rs @@ -19,4 +19,7 @@ pub const DANGLING: *const u8 = unsafe { ptr::NonNull::<u8>::dangling().as_ptr() // Right now, a zero offset from null is UB pub const NULL_OFFSET_ZERO: *const u8 = unsafe { ptr::null::<u8>().offset(0) }; //~NOTE +// Make sure that we don't panic when computing abs(offset*size_of::<T>()) +pub const UNDERFLOW_ABS: *const u8 = unsafe { (usize::MAX as *const u8).offset(isize::MIN) }; //~NOTE + fn main() {} diff --git a/src/test/ui/consts/offset_ub.stderr b/src/test/ui/consts/offset_ub.stderr index b2b77586a50..e808939682f 100644 --- a/src/test/ui/consts/offset_ub.stderr +++ b/src/test/ui/consts/offset_ub.stderr @@ -150,5 +150,20 @@ LL | intrinsics::offset(self, count) LL | pub const NULL_OFFSET_ZERO: *const u8 = unsafe { ptr::null::<u8>().offset(0) }; | ------------------------------------------------------------------------------- -error: aborting due to 10 previous errors +error: any use of this value will cause an error + --> $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL + | +LL | intrinsics::offset(self, count) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | unable to turn bytes into a pointer + | inside `std::ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL + | inside `UNDERFLOW_ABS` at $DIR/offset_ub.rs:23:47 + | + ::: $DIR/offset_ub.rs:23:1 + | +LL | pub const UNDERFLOW_ABS: *const u8 = unsafe { (usize::MAX as *const u8).offset(isize::MIN) }; + | --------------------------------------------------------------------------------------------- + +error: aborting due to 11 previous errors |
