Skip to content

NULL compares equal to 0 #4526

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

Merged
merged 1 commit into from
Apr 13, 2019
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions regression/cbmc/union13/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <assert.h>
#include <stdint.h>

int main()
{
union {
intptr_t i;
int *p;
} u;
u.i = 0;
assert(u.p == 0);
}
9 changes: 9 additions & 0 deletions regression/cbmc/union13/no-arch.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE broken-smt-backend
main.c
--arch none --little-endian
(Starting CEGAR Loop|^Generated 1 VCC\(s\), 1 remaining after simplification$)
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
8 changes: 8 additions & 0 deletions regression/cbmc/union13/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c

^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
17 changes: 16 additions & 1 deletion src/util/simplify_expr_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,8 +1357,23 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
{
if(expr.id() == ID_equal || expr.id() == ID_notequal)
{

// two constants compare equal when there values (as strings) are the same
// or both of them are pointers and both represent NULL in some way
bool equal = (tmp0_const->get_value() == tmp1_const->get_value());
if(
!equal && tmp0_const->type().id() == ID_pointer &&
tmp1_const->type().id() == ID_pointer)
{
if(
!config.ansi_c.NULL_is_zero && (tmp0_const->get_value() == ID_NULL ||
tmp1_const->get_value() == ID_NULL))
{
// if NULL is not zero on this platform, we really don't know what it
// is and therefore cannot simplify
return true;
}
equal = tmp0_const->is_zero() && tmp1_const->is_zero();
}
expr.make_bool(expr.id() == ID_equal ? equal : !equal);
return false;
}
Expand Down