Incorrect syntax of flexible array member size
Flexible array member defined with size zero or one
Description
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]; };
Risk
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.
Fix
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.
Examples
Result Information
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 |
Version History
Introduced in R2018b
See Also
Hard-coded buffer size
| Misuse of structure with
flexible array member
| Unprotected dynamic memory allocation
| Pointer access out of bounds
| Memory leak
| Find defects (-checkers)
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)