Menu ▾ ▴

#4221 Invalid flexible array members in unions and structures go undiagnosed

open
nobody
None
Front-end
5
2026-10-24
2026-10-24
No

1: Sample code that reproduces the problem.

union U
{
  int x;
  int a[];
};

2: Exact command used to run SDCC on this sample code.

sdcc -mz80 --std=c2y --nostdinc -S bug.c -o bug.asm

3: SDCC version tested.

SDCC 4.6.4 #17068 (Linux), tested 23 October 2026.

4: Observed and expected behaviour.

SDCC exits successfully and emits no diagnostic.

N3886 6.7.3.2 paragraph 3 permits an incomplete array member only as the last member of a struct with more than one named member. A union cannot have such a member. An unnamed bit-field does not supply a named member. A diagnostic is required.

Reference: N3886.

5: Additional checks.

The following structure is also accepted silently:

struct S
{
  unsigned : 1;
  int a[];
};

A structure with only int a[]; is already diagnosed, as is a flexible array member that is not last. Naming the preceding bit-field makes the structure valid, and it compiles silently on all five targets.

This differs from [bugs:#4197], which concerns unions containing structures that have valid flexible array members. Here the incomplete array is itself a union member, or the structure lacks another named member.

All invalid variants above were accepted without diagnostics on Z80, MCS51, STM8, PDK14 and MOS6502 in each of C99, C11, C23 and C2Y modes. GCC 13.3.0 with -std=c2x -pedantic-errors -fsyntax-only rejects them. GCC 16.2 and Clang were not tested.

Related

Bugs: #4197

Discussion


Log in to post a comment.