Skip to content

disallow flexible array members in unions #7296

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 1 commit 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
7 changes: 7 additions & 0 deletions regression/ansi-c/union/union_flexible_array_member.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// C11 6.7.2.1 §18 allows flexible array members in structures,
// but not unions.

union
{
char flexible_array_member[];
};
7 changes: 7 additions & 0 deletions regression/ansi-c/union/union_flexible_array_member.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CORE gcc-only
union_flexible_array_member.c

^EXIT=1$
^SIGNAL=0$
^CONVERSION ERROR$
--
41 changes: 30 additions & 11 deletions src/ansi-c/c_typecheck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1007,33 +1007,52 @@ void c_typecheck_baset::typecheck_compound_body(
}
}

// We allow an incomplete (C99) array as _last_ member!
// Zero-length is allowed everywhere.

if(type.id()==ID_struct ||
type.id()==ID_union)
// We allow an incomplete array as _last_ member of a struct,
// C11 6.7.2.1 §18. These are called "flexible array members".
// Zero-length (a gcc extension) is allowed everywhere.
if(type.id() == ID_struct)
{
for(struct_union_typet::componentst::iterator
it=components.begin();
it!=components.end();
it++)
{
typet &c_type=it->type();
typet &component_type = it->type();

if(c_type.id()==ID_array &&
to_array_type(c_type).is_incomplete())
if(
component_type.id() == ID_array &&
to_array_type(component_type).is_incomplete())
{
// needs to be last member
if(type.id()==ID_struct && it!=--components.end())
if(it != --components.end())
{
error().source_location=it->source_location();
error() << "flexible struct member must be last member" << eom;
throw 0;
}

// make it zero-length
to_array_type(c_type).size() = from_integer(0, c_index_type());
c_type.set(ID_C_flexible_array_member, true);
to_array_type(component_type).size() = from_integer(0, c_index_type());
component_type.set(ID_C_flexible_array_member, true);
}
}
}

// Flexible array members are not allowed in unions in ISO C.
// We allow this as an extension on Windows.
if(type.id() == ID_union && config.ansi_c.os != configt::ansi_ct::ost::OS_WIN)
{
for(const auto &component : components)
{
auto &component_type = component.type();

if(
component_type.id() == ID_array &&
to_array_type(component_type).is_incomplete())
{
throw invalid_source_file_exceptiont(
"flexible array members are not allowed in a union",
component.source_location());
}
}
}
Expand Down