Skip to content

Commit f893582

Browse files
committed
Expression initialiser: do not generate nil for empty union
Empty unions do not have a meaningful union_exprt as an initialiser, and thus the expression initialiser should make use of returning an optionalt{} instead. This test was generated using C-Reduce based on an example initially generated by CSmith.
1 parent fd8af8a commit f893582

File tree

6 files changed

+67
-14
lines changed

6 files changed

+67
-14
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
union {
2+
} a;
3+
main()
4+
{
5+
__assert_fail("", 2, __PRETTY_FUNCTION__);
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
--
8+
^warning: ignoring
9+
--
10+
CBMC would previously report "warning: ignoring nil" and eventually fail an
11+
invariant in src/solvers/flattening/boolbv_width.cpp:199 function: get_entry.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
union empty
2+
{
3+
};
4+
5+
struct S
6+
{
7+
int x;
8+
union empty e;
9+
int y;
10+
};
11+
12+
struct S s = { 1 };
13+
14+
int main()
15+
{
16+
assert(s.x == 1 && s.y == 0);
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/linking/static_lifetime_init.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,36 @@ static_lifetime_init(const irep_idt &identifier, symbol_tablet &symbol_table)
6868
return {}; // do not initialize
6969
}
7070

71-
exprt rhs;
71+
optionalt<exprt> rhs;
7272

7373
if((symbol.value.is_nil() && symbol.is_extern) ||
7474
symbol.value.id() == ID_nondet)
7575
{
7676
// Nondet initialise if not linked, or if explicitly requested.
7777
// Compilers would usually complain about the unlinked symbol case.
78-
const auto nondet = nondet_initializer(symbol.type, symbol.location, ns);
79-
CHECK_RETURN(nondet.has_value());
80-
rhs = *nondet;
78+
rhs = nondet_initializer(symbol.type, symbol.location, ns);
8179
}
8280
else if(symbol.value.is_nil())
8381
{
84-
const auto zero = zero_initializer(symbol.type, symbol.location, ns);
85-
CHECK_RETURN(zero.has_value());
86-
rhs = *zero;
82+
rhs = zero_initializer(symbol.type, symbol.location, ns);
8783
}
8884
else
8985
rhs = symbol.value;
9086

91-
code_assignt code(symbol.symbol_expr(), rhs);
92-
code.add_source_location() = symbol.location;
87+
if(rhs.has_value())
88+
{
89+
code_assignt code(symbol.symbol_expr(), *rhs);
90+
code.add_source_location() = symbol.location;
91+
92+
return std::move(code);
93+
}
94+
else
95+
{
96+
code_expressiont code{typecast_exprt{symbol.symbol_expr(), empty_typet{}}};
97+
code.add_source_location() = symbol.location;
9398

94-
return std::move(code);
99+
return std::move(code);
100+
}
95101
}
96102

97103
void static_lifetime_init(

src/util/expr_initializer.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ optionalt<exprt> expr_initializert<nondet>::expr_initializer_rec(
193193
code_value.add_source_location()=source_location;
194194
value.add_to_operands(std::move(code_value));
195195
}
196+
else if(c.type().id() == ID_union_tag &&
197+
ns.follow_tag(to_union_tag_type(c.type())).components().empty())
198+
{
199+
union_exprt empty_union{irep_idt{}, nil_exprt{}, c.type()};
200+
empty_union.add_source_location() = source_location;
201+
value.add_to_operands(std::move(empty_union));
202+
}
196203
else
197204
{
198205
const auto member = expr_initializer_rec(c.type(), source_location);
@@ -236,10 +243,8 @@ optionalt<exprt> expr_initializert<nondet>::expr_initializer_rec(
236243

237244
if(!found)
238245
{
239-
// stupid empty union
240-
union_exprt value(irep_idt(), nil_exprt(), type);
241-
value.add_source_location() = source_location;
242-
return std::move(value);
246+
// empty union or no member of known size
247+
return {};
243248
}
244249
else
245250
{

0 commit comments

Comments
 (0)