Skip to content

Commit cb43a34

Browse files
authored
Merge pull request #5664 from tautschnig/empty-union-initialiser
Do not fail an internal invariant when initialising an empty union
2 parents 750ab58 + 17ad15e commit cb43a34

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
union a {
2+
} b()
3+
{
4+
union a c = {0};
5+
}
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+
union member designator found for empty union
5+
^SIGNAL=0$
6+
^EXIT=(1|64)$
7+
--
8+
member designator is bounded by components size
9+
--
10+
This test previously failed an internal invariant, which is not the correct
11+
behaviour the problem can be triggered by user input.

src/ansi-c/c_typecheck_initializer.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,10 +478,21 @@ exprt::operandst::const_iterator c_typecheck_baset::do_designated_initializer(
478478
const union_typet::componentst &components=
479479
union_type.components();
480480

481-
DATA_INVARIANT(index<components.size(),
482-
"member designator is bounded by components size");
481+
if(components.empty())
482+
{
483+
error().source_location = value.source_location();
484+
error() << "union member designator found for empty union" << eom;
485+
throw 0;
486+
}
487+
else if(index >= components.size())
488+
{
489+
error().source_location = value.source_location();
490+
error() << "union member designator " << index << " out of bounds ("
491+
<< components.size() << ")" << eom;
492+
throw 0;
493+
}
483494

484-
const union_typet::componentt &component=union_type.components()[index];
495+
const union_typet::componentt &component = components[index];
485496

486497
if(dest->id()==ID_union &&
487498
dest->get(ID_component_name)==component.get_name())

0 commit comments

Comments
 (0)