Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated references from C11->C23

The C Standard, 6.7.3.2.1, paragraph 18 20 [ISO/IEC 9899:20112024], says:

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply.

...

Code Block
bgColor#ccccff
langc
#include <stdlib.h>
 
struct flex_array_struct {
  size_t num;
  int data[];
};
 
void func(void) {
  struct flex_array_struct *flex_struct;
  size_t array_size = 4;

  /* Dynamically allocate memory for the struct */
  flex_struct = (struct flex_array_struct *)malloc(
    sizeof(struct flex_array_struct)
    + sizeof(int) * array_size);
  if (flex_sructstruct == NULL) {
    /* Handle error */
  }

  /* Initialize structure */
  flex_struct->num = array_size;

  for (size_t i = 0; i < array_size; ++i) {
    flex_struct->data[i] = 0;
  }
}

...

Failure to use structures with flexible array members correctly can result in undefined behavior

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MEM33-C

Low

Unlikely

Low

P3

L3

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V
flexible-array-member-assignment
flexible-array-member-declaration
Fully checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-MEM33Fully implemented
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

LANG.STRUCT.DECL.FAM

Declaration of Flexible Array Member

Compass/ROSE
 

 



Can detect all of these

Cppcheck Premium

Include Page
Cppcheck Premium_V
Cppcheck Premium_V

premium-cert-mem33-cPartially  implemented
Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C1061, C1062, C1063, C1064
Klocwork
Include Page
Klocwork_V
Klocwork_V

MISRA.INCOMPLETE.STRUCT
MISRA.MEMB.FLEX_ARRAY.2012


LDRA tool suite
Include Page
LDRA_V
LDRA_V
649 S, 650 SFully
Implemented
implemented
Parasoft C/C++test

Include Page
Parasoft_V
Parasoft_V

CERT_C-MEM33-a
CERT_C-MEM33-b

Allocate structures containing a flexible array member dynamically
Do not copy instances of structures containing a flexible array member

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rule MEM33-CChecks for misuse of structure with flexible array member (rule fully covered)
RuleChecker

Include Page
RuleChecker_V
RuleChecker_V

flexible-array-member-assignment
flexible-array-member-declaration
Fully checked

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

Key here (explains table format and definitions)

Taxonomy

Taxonomy item

Relationship

CERT C Secure Coding StandardDCL38-C. Use the correct syntax when declaring a flexible array memberPrior to 2018-01-12: CERT: Unspecified Relationship

CERT-CWE Mapping Notes

Key here for mapping notes

CWE-401 and MEM33-CPP

There is no longer a C++ rule for MEM33-CPP. (In fact, all C++ rules from 30-50 are gone, because we changed the numbering system to be 50-99 for C++ rules.)

Bibliography

[ISO/IEC 9899:
2011
2024]Subclause 6.7.3.2
.1
, "Structure and Union Specifiers"
[JTC1/SC22/WG14 N791]

Solving the Struct Hack Problem

...


...

Image Modified Image Modified Image Modified