Skip to content

Commit 5edb9c9

Browse files
authored
Merge branch 'master' into master
2 parents ef59fc4 + 03cc719 commit 5edb9c9

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/operator.rs

+21
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,27 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
109109
err!(InvalidPointerMath)
110110
}
111111
}
112+
Gt | Ge if left.is_ptr() && right.is_bits() => {
113+
// "ptr >[=] integer" can be tested if the integer is small enough.
114+
let left = left.to_ptr().expect("we checked is_ptr");
115+
let right = right.to_bits(self.memory().pointer_size()).expect("we checked is_bits");
116+
let (_alloc_size, alloc_align) = self.memory()
117+
.get_size_and_align(left.alloc_id, InboundsCheck::MaybeDead)
118+
.expect("determining size+align of dead ptr cannot fail");
119+
let min_ptr_val = u128::from(alloc_align.bytes()) + u128::from(left.offset.bytes());
120+
let result = match bin_op {
121+
Gt => min_ptr_val > right,
122+
Ge => min_ptr_val >= right,
123+
_ => bug!(),
124+
};
125+
if result {
126+
// Definitely true!
127+
Ok((Scalar::from_bool(true), false))
128+
} else {
129+
// Sorry, can't tell.
130+
err!(InvalidPointerMath)
131+
}
132+
}
112133
// These work if the left operand is a pointer, and the right an integer
113134
Add | BitAnd | Sub | Rem if left.is_ptr() && right.is_bits() => {
114135
// Cast to i128 is fine as we checked the kind to be ptr-sized

tests/compile-fail/ptr_ge_integer.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let b = Box::new(0);
3+
let x = &*b as *const i32;
4+
// We cannot test if this is >= 64. After all, depending on the base address, that
5+
// might or might not be the case.
6+
assert!(x >= 64 as *const i32); //~ ERROR invalid arithmetic on pointers
7+
}

tests/run-pass/pointers.rs

+7
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,11 @@ fn main() {
8383
assert!(dangling != 5usize);
8484
assert!(dangling != 6usize);
8585
assert!(dangling != 7usize);
86+
87+
// Using inequality to do the comparison.
88+
assert!(dangling > 0);
89+
assert!(dangling > 1);
90+
assert!(dangling > 2);
91+
assert!(dangling > 3);
92+
assert!(dangling >= 4);
8693
}

0 commit comments

Comments
 (0)