Skip to content

Commit 8f68057

Browse files
committed
NULL compares equal to 0 when config.ansi_c.NULL_is_zero is set
When comparing constants, we just compared their string representations. This fails when one side of the (in)equality was NULL while the other side was 0, which is wrong when config.ansi_c.NULL_is_zero is set. Thus actually test both sides for being zero in this configuration.
1 parent 4205dcd commit 8f68057

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

regression/cbmc/union13/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
4+
int main()
5+
{
6+
union {
7+
intptr_t i;
8+
int *p;
9+
} u;
10+
u.i = 0;
11+
assert(u.p == 0);
12+
}

regression/cbmc/union13/no-arch.desc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE broken-smt-backend
2+
main.c
3+
--arch none --little-endian
4+
(Starting CEGAR Loop|^Generated 1 VCC\(s\), 1 remaining after simplification$)
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
^VERIFICATION SUCCESSFUL$
8+
--
9+
^warning: ignoring

regression/cbmc/union13/test.desc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/util/simplify_expr_int.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1357,8 +1357,23 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
13571357
{
13581358
if(expr.id() == ID_equal || expr.id() == ID_notequal)
13591359
{
1360-
1360+
// two constants compare equal when there values (as strings) are the same
1361+
// or both of them are pointers and both represent NULL in some way
13611362
bool equal = (tmp0_const->get_value() == tmp1_const->get_value());
1363+
if(
1364+
!equal && tmp0_const->type().id() == ID_pointer &&
1365+
tmp1_const->type().id() == ID_pointer)
1366+
{
1367+
if(
1368+
!config.ansi_c.NULL_is_zero && (tmp0_const->get_value() == ID_NULL ||
1369+
tmp1_const->get_value() == ID_NULL))
1370+
{
1371+
// if NULL is not zero on this platform, we really don't know what it
1372+
// is and therefore cannot simplify
1373+
return true;
1374+
}
1375+
equal = tmp0_const->is_zero() && tmp1_const->is_zero();
1376+
}
13621377
expr.make_bool(expr.id() == ID_equal ? equal : !equal);
13631378
return false;
13641379
}

0 commit comments

Comments
 (0)