Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further simplification of (in)equalities over pointers #7342

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions src/util/simplify_expr_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,19 @@ simplify_exprt::resultt<> simplify_exprt::simplify_inequality_no_constant(
new_expr.rhs() = simplify_node(new_expr.rhs());
return changed(simplify_inequality(new_expr)); // recursive call
}
else if(expr.op0().type().id() == ID_pointer)
{
exprt ptr_op0 = simplify_object(expr.op0()).expr;
exprt ptr_op1 = simplify_object(expr.op1()).expr;

if(ptr_op0 == ptr_op1)
{
pointer_offset_exprt offset_op0{expr.op0(), size_type()};
pointer_offset_exprt offset_op1{expr.op1(), size_type()};

return changed(simplify_rec(equal_exprt{offset_op0, offset_op1}));
}
}
}

return unchanged(expr);
Expand Down Expand Up @@ -1666,17 +1679,26 @@ simplify_exprt::resultt<> simplify_exprt::simplify_inequality_rhs_is_constant(
}
else if(expr.op0().id() == ID_plus)
{
// NULL + 1 == NULL is false
const plus_exprt &plus = to_plus_expr(expr.op0());
if(
plus.operands().size() == 2 && plus.op0().is_constant() &&
plus.op1().is_constant() &&
((is_null_pointer(to_constant_expr(plus.op0())) &&
!plus.op1().is_zero()) ||
(is_null_pointer(to_constant_expr(plus.op1())) &&
!plus.op0().is_zero())))
exprt offset =
simplify_rec(pointer_offset_exprt{expr.op0(), size_type()}).expr;
if(!offset.is_constant())
return unchanged(expr);

exprt ptr = simplify_object(expr.op0()).expr;
// NULL + N == NULL is N == 0
if(ptr.is_constant() && is_null_pointer(to_constant_expr(ptr)))
return make_boolean_expr(offset.is_zero());
// &x + N == NULL is false when the offset is in bounds
else if(auto address_of = expr_try_dynamic_cast<address_of_exprt>(ptr))
{
return false_exprt();
const auto object_size =
pointer_offset_size(address_of->object().type(), ns);
if(
object_size.has_value() &&
numeric_cast_v<mp_integer>(to_constant_expr(offset)) < *object_size)
{
return false_exprt();
}
}
}
}
Expand Down