Skip to content

Annotate flexible array members #6663

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

Merged
Merged
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
6 changes: 4 additions & 2 deletions src/analyses/goto_check_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1621,15 +1621,17 @@ void goto_check_ct::bounds_check_index(
? to_array_type(array_type).size()
: to_vector_type(array_type).size();

if(size.is_nil())
if(size.is_nil() && !array_type.get_bool(ID_C_flexible_array_member))
{
// Linking didn't complete, we don't have a size.
// Not clear what to do.
}
else if(size.id() == ID_infinity)
{
}
else if(size.is_zero() && expr.array().id() == ID_member)
else if(
expr.array().id() == ID_member &&
(size.is_zero() || array_type.get_bool(ID_C_flexible_array_member)))
{
// a variable sized struct member
//
Expand Down
4 changes: 2 additions & 2 deletions src/ansi-c/c_typecheck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,8 +1010,8 @@ void c_typecheck_baset::typecheck_compound_body(
}

// make it zero-length
c_type.id(ID_array);
c_type.set(ID_size, from_integer(0, c_index_type()));
to_array_type(c_type).size() = from_integer(0, c_index_type());
c_type.set(ID_C_flexible_array_member, true);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/util/irep_ids.def
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ IREP_ID_ONE(bitreverse)
IREP_ID_ONE(saturating_minus)
IREP_ID_ONE(saturating_plus)
IREP_ID_ONE(annotated_pointer_constant)
IREP_ID_TWO(C_flexible_array_member, #flexible_array_member)

// Projects depending on this code base that wish to extend the list of
// available ids should provide a file local_irep_ids.def in their source tree
Expand Down
5 changes: 5 additions & 0 deletions src/util/pointer_offset_size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ optionalt<exprt> size_of_expr(const typet &type, const namespacet &ns)
if(bytes > 0)
result = plus_exprt(result, from_integer(bytes, result.type()));
}
else if(c.type().get_bool(ID_C_flexible_array_member))
{
// flexible array members do not change the sizeof result
continue;
}
else
{
DATA_INVARIANT(
Expand Down
3 changes: 3 additions & 0 deletions src/util/simplify_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,9 @@ simplify_exprt::simplify_byte_extract(const byte_extract_exprt &expr)
if(comps.empty() || comps.back().type().id() != ID_array)
return false;

if(comps.back().type().get_bool(ID_C_flexible_array_member))
return true;

const auto size =
numeric_cast<mp_integer>(to_array_type(comps.back().type()).size());
return !size.has_value() || *size <= 1;
Expand Down