Skip to content

Stop asserting positive array size #4928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions regression/cbmc/Struct_Hack_Initialization/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <assert.h>

typedef struct stritem
{
unsigned nkey;
unsigned cas[];
} item;

int foo(item *it)
{
assert(it->cas[0] == 0);
return 0;
}
13 changes: 13 additions & 0 deletions regression/cbmc/Struct_Hack_Initialization/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CORE
main.c
--function foo --bounds-check --pointer-check
^\[foo.assertion.1\] line \d+ assertion it->cas\[0\] == 0: FAILURE$
^\[foo.array_bounds.3\] line \d+ array.cas upper bound in it->cas\[\(.*\)0\]: FAILURE$
^\[foo.array_bounds.2\] line \d+ array.cas dynamic object upper bound in it->cas\[\(.*\)0\]: FAILURE$
^\[foo.pointer_dereference.2\] line \d+ dereference failure: pointer invalid in it->cas: SUCCESS$
^\[foo.pointer_dereference.6\] line \d+ dereference failure: pointer outside object bounds in it->cas: FAILURE$
^EXIT=10$
^SIGNAL=0$
^VERIFICATION FAILED$
--
^warning: ignoring
1 change: 0 additions & 1 deletion src/ansi-c/c_nondet_symbol_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ void symbol_factoryt::gen_nondet_array_init(
const auto &size = array_type.size();
PRECONDITION(size.id() == ID_constant);
auto const array_size = numeric_cast_v<size_t>(to_constant_expr(size));
DATA_INVARIANT(array_size > 0, "Arrays should have positive size");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't properly taking this situation into account when writing this.

for(size_t index = 0; index < array_size; ++index)
{
gen_nondet_init(
Expand Down
15 changes: 13 additions & 2 deletions src/solvers/smt2/smt2_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2672,7 +2672,14 @@ void smt2_convt::convert_struct(const struct_exprt &expr)

// may need to flatten array-theory arrays in there
if(op.type().id() == ID_array)
flatten_array(op);
{
const array_typet &array_type = to_array_type(op.type());
const auto &size_expr = array_type.size();
CHECK_RETURN(size_expr.id() == ID_constant);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prevents using arrays with variable size.

if(numeric_cast_v<mp_integer>(to_constant_expr(size_expr)) != 0)
flatten_array(op);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In principle, this check would now need to be done in front of every call to flatten array, to make that a precondition of that function.
I think this is the wrong approach. I would let flatten_array handle this case.

else
convert_expr(op);

Expand Down Expand Up @@ -4526,7 +4533,11 @@ void smt2_convt::convert_type(const typet &type)
{
if(use_datatypes)
{
out << datatype_map.at(type);
const typet &struct_type = type.id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(type))
: type;
CHECK_RETURN(datatype_map.count(struct_type) > 0);
out << datatype_map.at(struct_type);
}
else
{
Expand Down