Flexible array member defined with size zero or one
This defect occurs when you do not use the standard C syntax to define a structure with a flexible array member.
Since C99, you can define a flexible array member with an unspecified size. For instance,
desc is a flexible array member in this
example:
struct record {
size_t len;
double desc[];
};struct record {
size_t len;
double desc[0];
};If you define flexible array members by using size zero or one, your implementation is
compiler-dependent. For compilers that do not recognize the syntax, an
int array of size one has buffer for one int
variable. If you try to write beyond this buffer, you can run into issues stemming from
array access out of bounds.
If you use the standard C syntax to define a flexible array member, your implementation is portable across all compilers conforming with the standard.
To implement a flexible array member in a structure, define an array of unspecified size. The structure must have one member besides the array and the array must be the last member of the structure.
| Group: Good Practice |
Language:C (checker disabled if the analysis runs on
C90 code indicated by the option -c-version c90) |
| Default: Off |
Command-Line Syntax:
FLEXIBLE_ARRAY_MEMBER_INCORRECT_SIZE |
| Impact: Low |
Find defects (-checkers) | Hard-coded buffer size | Memory leak | Misuse of structure with
flexible array member | Pointer access out of bounds | Unprotected dynamic memory allocation