Skip to content

Commit 76ae912

Browse files
author
Daniel Kroening
authored
Merge pull request #1158 from tautschnig/array-bounds
Support out-of-bounds checks on arrays of dynamic size
2 parents 652ea09 + d078dd8 commit 76ae912

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

regression/cbmc/dynamic_size1/main.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdlib.h>
2+
3+
int main()
4+
{
5+
unsigned x;
6+
7+
int *A=malloc(x*sizeof(int));
8+
9+
char *p=&A[1];
10+
11+
char c=*p;
12+
13+
return c;
14+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
--pointer-check
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
--
8+
^warning: ignoring
9+
^unknown or invalid type size:

src/pointer-analysis/value_set_dereference.cpp

+19-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Author: Daniel Kroening, [email protected]
1717

1818
#include <cassert>
1919

20+
#include <util/invariant.h>
2021
#include <util/string2int.h>
2122
#include <util/expr_util.h>
2223
#include <util/base_type.h>
@@ -858,16 +859,24 @@ bool value_set_dereferencet::memory_model_bytes(
858859
{
859860
// upper bound
860861
{
861-
mp_integer from_width=pointer_offset_size(from_type, ns);
862-
if(from_width<=0)
863-
throw "unknown or invalid type size:\n"+from_type.pretty();
864-
865-
mp_integer to_width=
866-
to_type.id()==ID_empty?0: pointer_offset_size(to_type, ns);
867-
if(to_width<0)
868-
throw "unknown or invalid type size:\n"+to_type.pretty();
869-
870-
exprt bound=from_integer(from_width-to_width, offset.type());
862+
exprt from_width=size_of_expr(from_type, ns);
863+
INVARIANT(
864+
from_width.is_not_nil(),
865+
"unknown or invalid type size:\n"+from_type.pretty());
866+
867+
exprt to_width=
868+
to_type.id()==ID_empty?
869+
from_integer(0, size_type()):size_of_expr(to_type, ns);
870+
INVARIANT(
871+
to_width.is_not_nil(),
872+
"unknown or invalid type size:\n"+to_type.pretty());
873+
INVARIANT(
874+
from_width.type()==to_width.type(),
875+
"type mismatch on result of size_of_expr");
876+
877+
minus_exprt bound(from_width, to_width);
878+
if(bound.type()!=offset.type())
879+
bound.make_typecast(offset.type());
871880

872881
binary_relation_exprt
873882
offset_upper_bound(offset, ID_gt, bound);

0 commit comments

Comments
 (0)