Skip to content

Commit b29cb7d

Browse files
committed
avoid catching errors
1 parent 8ec2506 commit b29cb7d

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/operator.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -397,21 +397,24 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
397397
.checked_mul(pointee_size)
398398
.ok_or_else(|| InterpError::Overflow(mir::BinOp::Mul))?;
399399
// Now let's see what kind of pointer this is.
400-
if let Ok(ptr) = self.force_ptr(ptr) {
401-
// Both old and new pointer must be in-bounds of a *live* allocation.
402-
// (Of the same allocation, but that part is trivial with our representation.)
403-
self.pointer_inbounds(ptr)?;
404-
let ptr = ptr.signed_offset(offset, self)?;
405-
self.pointer_inbounds(ptr)?;
406-
Ok(Scalar::Ptr(ptr))
407-
} else {
408-
// A "true" integer pointer. They can only be offset by 0, and we pretend there
409-
// is a little zero-sized allocation here.
410-
if offset == 0 {
411-
Ok(ptr)
412-
} else {
413-
err!(InvalidPointerMath)
400+
let ptr = if offset == 0 {
401+
match ptr {
402+
Scalar::Ptr(ptr) => ptr,
403+
Scalar::Raw { .. } => {
404+
// Offset 0 on an integer. We accept that, pretending there is
405+
// a little zero-sized allocation here.
406+
return Ok(ptr);
407+
}
414408
}
415-
}
409+
} else {
410+
// Offset > 0. We *require* a pointer.
411+
self.force_ptr(ptr)?
412+
};
413+
// Both old and new pointer must be in-bounds of a *live* allocation.
414+
// (Of the same allocation, but that part is trivial with our representation.)
415+
self.pointer_inbounds(ptr)?;
416+
let ptr = ptr.signed_offset(offset, self)?;
417+
self.pointer_inbounds(ptr)?;
418+
Ok(Scalar::Ptr(ptr))
416419
}
417420
}

tests/compile-fail/ptr_offset_int_plus_int.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// error-pattern: invalid arithmetic on pointers
1+
// error-pattern: tried to interpret some bytes as a pointer
22

33
fn main() {
44
// Can't offset an integer pointer by non-zero offset.

0 commit comments

Comments
 (0)