Skip to content

Commit 673ef5f

Browse files
committed
Fix C frontend array type sync in goto program
Update goto program expressions to use updated array type from symbol table when arrays declared without length are later defined per C standard 6.9.2 Co-authored-by: Kiro autonomous agent Fixes: #5022
1 parent 76dd20f commit 673ef5f

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

src/goto-symex/field_sensitivity.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,7 @@ exprt field_sensitivityt::apply(
215215
bool was_l2 = !tmp.get_level_2().empty();
216216
exprt l2_size =
217217
state.rename(to_array_type(index.array().type()).size(), ns).get();
218-
if(l2_size.is_nil() && index.array().id() == ID_symbol)
219-
{
220-
// In case the array type was incomplete, attempt to retrieve it from
221-
// the symbol table.
222-
const symbolt *array_from_symbol_table = ns.get_symbol_table().lookup(
223-
to_symbol_expr(index.array()).get_identifier());
224-
if(array_from_symbol_table != nullptr)
225-
l2_size = to_array_type(array_from_symbol_table->type).size();
226-
}
227-
218+
PRECONDITION(l2_size.is_not_nil());
228219
if(
229220
l2_size.is_constant() &&
230221
numeric_cast_v<mp_integer>(to_constant_expr(l2_size)) <=

src/linking/static_lifetime_init.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,48 +52,55 @@ static std::optional<codet> static_lifetime_init(
5252
if(symbol.type.id() == ID_code || symbol.type.id() == ID_empty)
5353
return {};
5454

55-
if(symbol.type.id() == ID_array && to_array_type(symbol.type).size().is_nil())
55+
// Get a writable reference to the symbol in case we need to update it
56+
symbolt *symbol_ptr = &symbol_table.get_writeable_ref(identifier);
57+
58+
if(
59+
symbol_ptr->type.id() == ID_array &&
60+
to_array_type(symbol_ptr->type).size().is_nil())
5661
{
5762
// C standard 6.9.2, paragraph 5
5863
// adjust the type to an array of size 1
59-
symbolt &writable_symbol = symbol_table.get_writeable_ref(identifier);
60-
writable_symbol.type = symbol.type;
61-
writable_symbol.type.set(ID_size, from_integer(1, size_type()));
64+
symbol_ptr->type.set(ID_size, from_integer(1, size_type()));
6265
}
6366

6467
if(
65-
(symbol.type.id() == ID_struct_tag &&
66-
ns.follow_tag(to_struct_tag_type(symbol.type)).is_incomplete()) ||
67-
(symbol.type.id() == ID_union_tag &&
68-
ns.follow_tag(to_union_tag_type(symbol.type)).is_incomplete()))
68+
(symbol_ptr->type.id() == ID_struct_tag &&
69+
ns.follow_tag(to_struct_tag_type(symbol_ptr->type)).is_incomplete()) ||
70+
(symbol_ptr->type.id() == ID_union_tag &&
71+
ns.follow_tag(to_union_tag_type(symbol_ptr->type)).is_incomplete()))
6972
{
7073
return {}; // do not initialize
7174
}
7275

7376
exprt rhs;
7477

75-
if((symbol.value.is_nil() && symbol.is_extern) ||
76-
symbol.value.id() == ID_nondet)
78+
if(
79+
(symbol_ptr->value.is_nil() && symbol_ptr->is_extern) ||
80+
symbol_ptr->value.id() == ID_nondet)
7781
{
78-
if(symbol.value.get_bool(ID_C_no_nondet_initialization))
82+
if(symbol_ptr->value.get_bool(ID_C_no_nondet_initialization))
7983
return {};
8084

8185
// Nondet initialise if not linked, or if explicitly requested.
8286
// Compilers would usually complain about the unlinked symbol case.
83-
const auto nondet = nondet_initializer(symbol.type, symbol.location, ns);
87+
const auto nondet =
88+
nondet_initializer(symbol_ptr->type, symbol_ptr->location, ns);
8489
CHECK_RETURN(nondet.has_value());
8590
rhs = *nondet;
8691
}
87-
else if(symbol.value.is_nil())
92+
else if(symbol_ptr->value.is_nil())
8893
{
89-
const auto zero = zero_initializer(symbol.type, symbol.location, ns);
94+
const auto zero =
95+
zero_initializer(symbol_ptr->type, symbol_ptr->location, ns);
9096
CHECK_RETURN(zero.has_value());
9197
rhs = *zero;
9298
}
9399
else
94-
rhs = symbol.value;
100+
rhs = symbol_ptr->value;
95101

96-
return code_frontend_assignt{symbol.symbol_expr(), rhs, symbol.location};
102+
return code_frontend_assignt{
103+
symbol_ptr->symbol_expr(), rhs, symbol_ptr->location};
97104
}
98105

99106
void static_lifetime_init(

0 commit comments

Comments
 (0)