Rule Definition
Flexible array members are most likely to be used in conjunction with dynamic memory allocation. The presence of flexible array members modifies the behavior of the sizeof operator in ways that might not be expected by a programmer. The assignment of a structure that contains a flexible array member to another structure of the same type may not behave in the expected manner as it copies only those elements up to but not including the start of the flexible array member.
Remediation
Define the size of array members.
Violation Code Sample
#include <stdlib.h>
struct s
{
uint16_t len;
uint32_t data[ ]; /* Non-compliant - flexible array member */
} str;
struct s *copy ( struct s *s1 )
{
struct s *s2;
/* Omit malloc ( ) return check for brevity */
s2 = malloc ( sizeof ( struct s ) + ( s1->len * sizeof ( uint32_t ) ) );
*s2 = *s1; /* Only copies s1->len */
return
Fixed Code Sample
Reference
MISRA C 2012, 18.7: Flexible array members shall not be declared
Related Technologies
Technical Criterion
Complexity - Dynamic Instantiation
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.