Skip to content

Commit 8b0c14f

Browse files
committed
ptr_offset: handle negative offsets
1 parent b2a2f4d commit 8b0c14f

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/operator.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,18 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
112112
.ok_or_else(|| err_panic!(Overflow(mir::BinOp::Mul)))?;
113113
// We do this first, to rule out overflows.
114114
let offset_ptr = ptr.ptr_signed_offset(offset, self)?;
115-
// What we need to check is that starting at `ptr`,
116-
// we could do an access of size `offset`. Alignment does not matter.
115+
// What we need to check is that starting at `min(ptr, offset_ptr)`,
116+
// we could do an access of size `abs(offset)`. Alignment does not matter.
117+
let (min_ptr, abs_offset) = if offset >= 0 {
118+
(ptr, u64::try_from(offset).unwrap())
119+
} else {
120+
// Negative offset.
121+
// If the negation overflows, the result will be negative so the try_from will fail.
122+
(offset_ptr, u64::try_from(-offset).unwrap())
123+
};
117124
self.memory.check_ptr_access_align(
118-
ptr,
119-
Size::from_bytes(u64::try_from(offset).unwrap()),
125+
min_ptr,
126+
Size::from_bytes(abs_offset),
120127
None,
121128
CheckInAllocMsg::InboundsTest,
122129
)?;

0 commit comments

Comments
 (0)