Skip to content

Commit db1652e

Browse files
committed
fix the way we detect overflow for inbounds arithmetic (and tweak the error message)
1 parent 5d5c97a commit db1652e

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

compiler/rustc_const_eval/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ const_eval_partial_pointer_copy =
277277
const_eval_partial_pointer_overwrite =
278278
unable to overwrite parts of a pointer in memory at {$ptr}
279279
const_eval_pointer_arithmetic_overflow =
280-
overflowing in-bounds pointer arithmetic
280+
overflowing pointer arithmetic: the total offset in bytes does not fit in an `isize`
281281
const_eval_pointer_arithmetic_test = out-of-bounds pointer arithmetic
282282
const_eval_pointer_out_of_bounds =
283283
{$bad_pointer_message}: {const_eval_expected_inbounds_pointer}, but got {$pointer} {$ptr_offset_is_neg ->

compiler/rustc_const_eval/src/interpret/operator.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::{bug, mir, span_bug};
77
use rustc_span::symbol::sym;
88
use tracing::trace;
99

10-
use super::{err_ub, throw_ub, ImmTy, InterpCx, Machine, MemPlaceMeta};
10+
use super::{throw_ub, ImmTy, InterpCx, Machine, MemPlaceMeta};
1111

1212
impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
1313
fn three_way_compare<T: Ord>(&self, lhs: T, rhs: T) -> ImmTy<'tcx, M::Provenance> {
@@ -298,17 +298,23 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
298298
// Pointer ops that are always supported.
299299
Offset => {
300300
let ptr = left.to_scalar().to_pointer(self)?;
301-
let offset_count = right.to_scalar().to_target_isize(self)?;
302301
let pointee_ty = left.layout.ty.builtin_deref(true).unwrap();
302+
let pointee_layout = self.layout_of(pointee_ty)?;
303+
assert!(pointee_layout.abi.is_sized());
303304

304305
// We cannot overflow i64 as a type's size must be <= isize::MAX.
305-
let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
306-
// The computed offset, in bytes, must not overflow an isize.
307-
// `checked_mul` enforces a too small bound, but no actual allocation can be big enough for
308-
// the difference to be noticeable.
309-
let offset_bytes =
310-
offset_count.checked_mul(pointee_size).ok_or(err_ub!(PointerArithOverflow))?;
306+
let pointee_size = i64::try_from(pointee_layout.size.bytes()).unwrap();
307+
let pointee_size = ImmTy::from_int(pointee_size, right.layout);
308+
// Multiply element size and element count.
309+
let (val, overflowed) = self
310+
.binary_op(mir::BinOp::MulWithOverflow, right, &pointee_size)?
311+
.to_scalar_pair();
312+
// This must not overflow.
313+
if overflowed.to_bool()? {
314+
throw_ub!(PointerArithOverflow)
315+
}
311316

317+
let offset_bytes = val.to_target_isize(self)?;
312318
let offset_ptr = self.ptr_offset_inbounds(ptr, offset_bytes)?;
313319
Ok(ImmTy::from_scalar(Scalar::from_maybe_pointer(offset_ptr, self), left.layout))
314320
}

tests/ui/consts/offset_ub.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ LL | pub const AFTER_ARRAY: *const u8 = unsafe { [0u8; 100].as_ptr().offset(101)
4040
error[E0080]: evaluation of constant value failed
4141
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
4242
|
43-
= note: overflowing in-bounds pointer arithmetic
43+
= note: overflowing pointer arithmetic: the total offset in bytes does not fit in an `isize`
4444
|
4545
note: inside `std::ptr::const_ptr::<impl *const u16>::offset`
4646
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
@@ -53,7 +53,7 @@ LL | pub const OVERFLOW: *const u16 = unsafe { [0u16; 1].as_ptr().offset(isize::
5353
error[E0080]: evaluation of constant value failed
5454
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
5555
|
56-
= note: overflowing in-bounds pointer arithmetic
56+
= note: overflowing pointer arithmetic: the total offset in bytes does not fit in an `isize`
5757
|
5858
note: inside `std::ptr::const_ptr::<impl *const u16>::offset`
5959
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL

0 commit comments

Comments
 (0)