Merge branch 'master' into master

This commit is contained in:
Oliver Scherer 2019-06-21 12:37:13 +02:00 committed by GitHub
commit 5edb9c915f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View File

@ -109,6 +109,27 @@ fn ptr_op(
err!(InvalidPointerMath)
}
}
Gt | Ge if left.is_ptr() && right.is_bits() => {
// "ptr >[=] integer" can be tested if the integer is small enough.
let left = left.to_ptr().expect("we checked is_ptr");
let right = right.to_bits(self.memory().pointer_size()).expect("we checked is_bits");
let (_alloc_size, alloc_align) = self.memory()
.get_size_and_align(left.alloc_id, InboundsCheck::MaybeDead)
.expect("determining size+align of dead ptr cannot fail");
let min_ptr_val = u128::from(alloc_align.bytes()) + u128::from(left.offset.bytes());
let result = match bin_op {
Gt => min_ptr_val > right,
Ge => min_ptr_val >= right,
_ => bug!(),
};
if result {
// Definitely true!
Ok((Scalar::from_bool(true), false))
} else {
// Sorry, can't tell.
err!(InvalidPointerMath)
}
}
// These work if the left operand is a pointer, and the right an integer
Add | BitAnd | Sub | Rem if left.is_ptr() && right.is_bits() => {
// Cast to i128 is fine as we checked the kind to be ptr-sized

View File

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

View File

@ -83,4 +83,11 @@ fn main() {
assert!(dangling != 5usize);
assert!(dangling != 6usize);
assert!(dangling != 7usize);
// Using inequality to do the comparison.
assert!(dangling > 0);
assert!(dangling > 1);
assert!(dangling > 2);
assert!(dangling > 3);
assert!(dangling >= 4);
}