Skip to content

Commit b44fb2b

Browse files
committed
C front-end: fix initialisation of large arrays
In 375e9a8 the construction of zero initialisers was switched to array_of expressions for arrays with more than 1000 elements. This is beneficial to avoid running out of memory, but didn't take into account arrays with designated initialisers, which this patch fixes.
1 parent 48b8c90 commit b44fb2b

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// an array that is larger than MAX_FLATTENED_ARRAY_SIZE
2+
static const unsigned char data[256 * 4] = {0x0, 0x0};
3+
4+
int main()
5+
{
6+
return data[0];
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring

src/ansi-c/c_typecheck_initializer.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,25 @@ exprt::operandst::const_iterator c_typecheck_baset::do_designated_initializer(
397397
if(full_type.id()==ID_array ||
398398
full_type.id()==ID_vector)
399399
{
400+
// zero_initializer may have built an array_of expression for a large
401+
// array; as we have a designated initializer we need to have an array of
402+
// individual objects
403+
if(dest->id() == ID_array_of)
404+
{
405+
const array_typet array_type = to_array_type(dest->type());
406+
const auto array_size = numeric_cast<mp_integer>(array_type.size());
407+
if(!array_size.has_value())
408+
{
409+
error().source_location = value.source_location();
410+
error() << "cannot zero-initialize array with subtype '"
411+
<< to_string(full_type.subtype()) << "'" << eom;
412+
throw 0;
413+
}
414+
const exprt zero = to_array_of_expr(*dest).what();
415+
*dest = array_exprt{{}, array_type};
416+
dest->operands().resize(numeric_cast_v<std::size_t>(*array_size), zero);
417+
}
418+
400419
if(index>=dest->operands().size())
401420
{
402421
if(full_type.id()==ID_array &&

0 commit comments

Comments
 (0)