Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.MEMB.FLEX_ARRAY.2012

MISRA C 2012 Rule 18.7: Flexible array members shall not be declared

C99 [Undefined 59]

Category: Required

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Rationale

Flexible array members are most likely to be used in conjunction with dynamic memory allocation which is banned by Dir 4.12 and Rule 21.3.

The presence of flexible array members modifies the behaviour 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.

Example

#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 s2; 
}

See also

Dir 4.12, Rule 21.3