Skip to content

Commit 15bf786

Browse files
committed
NULL compares equal to 0
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 9f63d79 commit 15bf786

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

regression/cbmc/union13/main.c

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

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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,8 +1357,13 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
13571357
{
13581358
if(expr.id() == ID_equal || expr.id() == ID_notequal)
13591359
{
1360-
1361-
bool equal = (tmp0_const->get_value() == tmp1_const->get_value());
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
1362+
const bool equal =
1363+
(tmp0_const->get_value() == tmp1_const->get_value()) ||
1364+
(tmp0_const->type().id() == ID_pointer &&
1365+
tmp1_const->type().id() == ID_pointer && config.ansi_c.NULL_is_zero &&
1366+
tmp0_const->is_zero() && tmp1_const->is_zero());
13621367
expr.make_bool(expr.id() == ID_equal ? equal : !equal);
13631368
return false;
13641369
}

0 commit comments

Comments
 (0)