Add a debug check for ordering, and check for isize overflow in CTFE
This commit is contained in:
parent
e76b3f3b5b
commit
4bb15b3797
@ -365,10 +365,17 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
} else {
|
} else {
|
||||||
usize_layout
|
usize_layout
|
||||||
};
|
};
|
||||||
let a_offset = ImmTy::from_uint(a_offset.bytes(), usize_layout);
|
|
||||||
let b_offset = ImmTy::from_uint(b_offset.bytes(), usize_layout);
|
// The subtraction is always done in `isize` to enforce
|
||||||
let (val, _overflowed, _ty) =
|
// the "no more than `isize::MAX` apart" requirement.
|
||||||
|
let a_offset = ImmTy::from_uint(a_offset.bytes(), isize_layout);
|
||||||
|
let b_offset = ImmTy::from_uint(b_offset.bytes(), isize_layout);
|
||||||
|
let (val, overflowed, _ty) =
|
||||||
self.overflowing_binary_op(BinOp::Sub, &a_offset, &b_offset)?;
|
self.overflowing_binary_op(BinOp::Sub, &a_offset, &b_offset)?;
|
||||||
|
if overflowed {
|
||||||
|
throw_ub_format!("Pointers were too far apart for {}", intrinsic_name);
|
||||||
|
}
|
||||||
|
|
||||||
let pointee_layout = self.layout_of(substs.type_at(0))?;
|
let pointee_layout = self.layout_of(substs.type_at(0))?;
|
||||||
let val = ImmTy::from_scalar(val, ret_layout);
|
let val = ImmTy::from_scalar(val, ret_layout);
|
||||||
let size = ImmTy::from_int(pointee_layout.size.bytes(), ret_layout);
|
let size = ImmTy::from_int(pointee_layout.size.bytes(), ret_layout);
|
||||||
|
@ -678,6 +678,10 @@ impl<T: ?Sized> *const T {
|
|||||||
where
|
where
|
||||||
T: Sized,
|
T: Sized,
|
||||||
{
|
{
|
||||||
|
// SAFETY: The comparison has no side-effects, and the intrinsic
|
||||||
|
// does this check internally in the CTFE implementation.
|
||||||
|
unsafe { assert_unsafe_precondition!(self >= origin) };
|
||||||
|
|
||||||
let pointee_size = mem::size_of::<T>();
|
let pointee_size = mem::size_of::<T>();
|
||||||
assert!(0 < pointee_size && pointee_size <= isize::MAX as usize);
|
assert!(0 < pointee_size && pointee_size <= isize::MAX as usize);
|
||||||
// SAFETY: the caller must uphold the safety contract for `ptr_offset_from_unsigned`.
|
// SAFETY: the caller must uphold the safety contract for `ptr_offset_from_unsigned`.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user