...
- The incomplete array type must be the last element within the structure.
- There cannot be an array of structures that contain flexible array members.
- Structures that contain a flexible array member cannot be used as a member in the middle of another structure.
- The
sizeof
operator cannot be applied to a flexible array.
Automated Detection
The tool Compass / ROSE could try flagging violations of this rule simply by searching for structs where the last element is an array, with a small index (0 or 1). Alternately, it could flag violations by searching for arguments to malloc()
of the form sizeof( struct) * sizeof(
arraytype ) - 1
.
Risk Assessment
Failing to use the correct syntax can result in undefined behavior, although the incorrect syntax will work on most implementations.
...