Skip to content

Commit 81aea22

Browse files
author
kroening
committed
Any compile-time integer constant expression evaluating
to 0 is a null pointer constant git-svn-id: svn+ssh://svn.cprover.org/srv/svn/cbmc/trunk@3613 6afb6bc1-c8e4-404c-8f48-9ae832c5b171
1 parent 967bc2b commit 81aea22

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

regression/cbmc/null1/main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#define STATIC_ASSERT(condition) \
2+
int some_array##__LINE__[(condition) ? 1 : -1];
3+
4+
STATIC_ASSERT((void*)0==(void*)(1-1));
5+
6+
int main()
7+
{
8+
assert((void*)0==(void*)('a'-'a'));
9+
return 0;
10+
}

regression/cbmc/null1/test.desc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
--all-claims --no-simplify
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[main\.1\] assertion NULL == NULL: OK$
7+
^\*\* 0 of 1 failed (1 iterations)$
8+
--
9+
^warning: ignoring

src/ansi-c/c_typecast.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Author: Daniel Kroening, [email protected]
1313
#include <util/std_expr.h>
1414
#include <util/base_type.h>
1515
#include <util/symbol.h>
16+
#include <util/simplify_expr.h>
1617

1718
#include "c_typecast.h"
1819
#include "c_types.h"
@@ -549,7 +550,7 @@ void c_typecastt::implicit_typecast_followed(
549550
{
550551
// special case: 0 == NULL
551552

552-
if(expr.is_zero() && (
553+
if(simplify_expr(expr, ns).is_zero() && (
553554
src_type.id()==ID_unsignedbv ||
554555
src_type.id()==ID_signedbv ||
555556
src_type.id()==ID_natural ||

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ void c_typecheck_baset::typecheck_expr_typecast(exprt &expr)
11791179

11801180
// special case: NULL
11811181
if(expr_type.id()==ID_pointer &&
1182-
op.is_zero())
1182+
simplify_expr(op, *this).is_zero())
11831183
{
11841184
// zero typecasted to a pointer is NULL
11851185
constant_exprt result(ID_NULL, expr_type);
@@ -1400,31 +1400,18 @@ void c_typecheck_baset::typecheck_expr_rel(exprt &expr)
14001400
}
14011401
else
14021402
{
1403-
// pointer and zero
1404-
if(type0.id()==ID_pointer && op1.is_zero())
1405-
{
1406-
op1=constant_exprt(type0);
1407-
op1.set(ID_value, ID_NULL);
1408-
return;
1409-
}
1410-
1411-
if(type1.id()==ID_pointer && op0.is_zero())
1412-
{
1413-
op0=constant_exprt(type1);
1414-
op0.set(ID_value, ID_NULL);
1415-
return;
1416-
}
1417-
14181403
// pointer and integer
14191404
if(type0.id()==ID_pointer && is_number(type1))
14201405
{
14211406
op1.make_typecast(type0);
1407+
typecheck_expr_typecast(op1);
14221408
return;
14231409
}
14241410

14251411
if(type1.id()==ID_pointer && is_number(type0))
14261412
{
14271413
op0.make_typecast(type1);
1414+
typecheck_expr_typecast(op0);
14281415
return;
14291416
}
14301417

src/cpp/cpp_typecheck_conversions.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Module: C++ Language Type Checking
1313
#include <util/expr_util.h>
1414
#include <util/std_types.h>
1515
#include <util/std_expr.h>
16+
#include <util/simplify_expr.h>
1617

1718
#include <ansi-c/c_qualifiers.h>
1819
#include <ansi-c/c_types.h>
@@ -560,7 +561,7 @@ bool cpp_typecheckt::standard_conversion_pointer(
560561
return false;
561562

562563
// integer 0 to NULL pointer conversion?
563-
if(expr.is_zero() &&
564+
if(simplify_expr(expr, *this).is_zero() &&
564565
expr.type().id()!=ID_pointer)
565566
{
566567
new_expr = expr;
@@ -2088,7 +2089,7 @@ bool cpp_typecheckt::reinterpret_typecast(
20882089
&& !is_reference(type))
20892090
{
20902091
// integer to pointer
2091-
if(e.is_zero())
2092+
if(simplify_expr(e, *this).is_zero())
20922093
{
20932094
// NULL
20942095
new_expr = e;

0 commit comments

Comments
 (0)