Skip to content

Commit e2872a3

Browse files
committed
Auto merge of #1853 - RalfJung:negative-offsets, r=RalfJung
better errors for negative out-of-bounds offsets This is the Miri side of rust-lang/rust#87224
2 parents 37974e6 + 6328677 commit e2872a3

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a72c360a30f9a8160e4f40340cecc9b1ce979cd7
1+
718d53b0cb7dde93499cb92950d60b412f5a3d05

src/stacked_borrows.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -623,14 +623,14 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
623623
let orig_tag = ptr.provenance.sb;
624624

625625
// Ensure we bail out if the pointer goes out-of-bounds (see miri#1050).
626-
let (allocation_size, _) =
626+
let (alloc_size, _) =
627627
this.memory.get_size_and_align(alloc_id, AllocCheck::Dereferenceable)?;
628-
if base_offset + size > allocation_size {
628+
if base_offset + size > alloc_size {
629629
throw_ub!(PointerOutOfBounds {
630630
alloc_id,
631-
offset: base_offset,
632-
size,
633-
allocation_size,
631+
alloc_size,
632+
ptr_offset: this.machine_usize_to_isize(base_offset.bytes()),
633+
ptr_size: size,
634634
msg: CheckInAllocMsg::InboundsTest
635635
});
636636
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// error-pattern: pointer to 1 byte starting at offset -1 is out-of-bounds
2+
fn main() {
3+
let v = [0i8; 4];
4+
let x = &v as *const i8;
5+
let x = unsafe { x.offset(-1) };
6+
panic!("this should never print: {:?}", x);
7+
}

0 commit comments

Comments
 (0)