File tree 3 files changed +53
-4
lines changed
regression/cbmc/ptr_arithmetic_on_null
3 files changed +53
-4
lines changed Original file line number Diff line number Diff line change
1
+ #include <stdlib.h>
2
+
3
+ void main ()
4
+ {
5
+ assert (NULL != (NULL + 1 ));
6
+ assert (NULL != (NULL - 1 ));
7
+
8
+ int offset ;
9
+ __CPROVER_assume (offset != 0 );
10
+ assert (NULL != (NULL + offset ));
11
+
12
+ assert (NULL - NULL == 0 );
13
+
14
+ int * ptr ;
15
+ assert (ptr - NULL == 0 );
16
+ ptr = NULL ;
17
+ assert ((ptr - 1 ) + 1 == NULL );
18
+
19
+ ptr = nondet () ? NULL : malloc (1 );
20
+ assert ((ptr - 1 ) + 1 == (NULL + 1 ) - 1 );
21
+ }
Original file line number Diff line number Diff line change
1
+ CORE
2
+ main.c
3
+
4
+ ^\[main.assertion.1\] line .* assertion \(void \*\)0 != \(void \*\)0 \+ \(.*\)1: SUCCESS$
5
+ ^\[main.assertion.2\] line .* assertion \(void \*\)0 != \(void \*\)0 - \(.*\)1: SUCCESS$
6
+ ^\[main.assertion.3\] line .* assertion \(void \*\)0 != \(void \*\)0 \+ \(.*\)offset: SUCCESS$
7
+ ^\[main.assertion.4\] line .* assertion \(void \*\)0 - \(void \*\)0 == \(.*\)0: SUCCESS$
8
+ ^\[main.assertion.5\] line .* assertion ptr - \(void \*\)0 == \(.*\)0: FAILURE$
9
+ ^\[main.assertion.6\] line .* assertion \(ptr - \(.*\)1\) \+ \(.*\)1 == \(\(.* \*\)NULL\): SUCCESS$
10
+ ^\[main.assertion.7\] line .* assertion \(ptr - \(.*\)1\) \+ \(.*\)1 == \(\(.* \*\)NULL\): FAILURE$
11
+ ^EXIT=10$
12
+ ^SIGNAL=0$
13
+ ^VERIFICATION FAILED$
14
+ --
15
+ --
16
+ This test case checks that pointer arithmetic on NULL pointers are correctly
17
+ encoded by the SMT2 translation routines.
Original file line number Diff line number Diff line change @@ -3162,18 +3162,29 @@ void smt2_convt::convert_plus(const plus_exprt &expr)
3162
3162
p.type ().id () == ID_pointer,
3163
3163
" one of the operands should have pointer type" );
3164
3164
3165
- const auto element_size = pointer_offset_size (expr.type ().subtype (), ns);
3166
- CHECK_RETURN (element_size.has_value () && *element_size >= 1 );
3165
+ mp_integer element_size;
3166
+ if (expr.type ().subtype ().id () == ID_empty)
3167
+ {
3168
+ // This is a gcc extension.
3169
+ // https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Pointer-Arith.html
3170
+ element_size = 1 ;
3171
+ }
3172
+ else
3173
+ {
3174
+ auto element_size_opt = pointer_offset_size (expr.type ().subtype (), ns);
3175
+ CHECK_RETURN (element_size_opt.has_value () && *element_size_opt >= 1 );
3176
+ element_size = *element_size_opt;
3177
+ }
3167
3178
3168
3179
out << " (bvadd " ;
3169
3180
convert_expr (p);
3170
3181
out << " " ;
3171
3182
3172
- if (* element_size >= 2 )
3183
+ if (element_size >= 2 )
3173
3184
{
3174
3185
out << " (bvmul " ;
3175
3186
convert_expr (i);
3176
- out << " (_ bv" << * element_size << " " << boolbv_width (expr.type ())
3187
+ out << " (_ bv" << element_size << " " << boolbv_width (expr.type ())
3177
3188
<< " ))" ;
3178
3189
}
3179
3190
else
You can’t perform that action at this time.
0 commit comments