Misuse of structure with flexible array member
Memory allocation ignores flexible array member
Description
This defect occurs when:
You define an object with a flexible array member of unknown size at compilation time.
You make an assignment between structures with a flexible array member without using
memcpy()
or a similar function.You use a structure with a flexible array member as an argument to a function and pass the argument by value.
Your function returns a structure with a flexible array member.
A flexible array member has no array size specified and is the last element of a structure with at least two named members.
Risk
If the size of the flexible array member is not defined, it is ignored when allocating memory for the containing structure. Accessing such a structure has undefined behavior.
Fix
Use
malloc()
or a similar function to allocate memory for a structure with a flexible array member.Use
memcpy()
or a similar function to copy a structure with a flexible array member.Pass a structure with a flexible array member as a function argument by pointer.
Examples
Result Information
Group: Programming |
Language: C | C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax:
FLEXIBLE_ARRAY_MEMBER_STRUCT_MISUSE
|
Impact: Low |
Version History
Introduced in R2017b
See Also
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)