Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
The C99 standard \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] introduces flexible array members into the language. While flexible array members are a useful addition, they need to be understood and used with care. 

...

To avoid the potential for undefined behavior, structures that contain a flexible array member should always be dynamically allocated and operated on dynamically.

Noncompliant Code Example (Storage Allocation)

This noncompliant code example statically allocates storage for a structure containing a flexible array member.

...

Wiki Markup
The problem with this code is that the {{flexArrayStruct}} does not actually reserve space for the integer array data: - itIt can't asbecause the size hasn't been specified. Consequently, while initializing the {{num}} member to zero is allowed, attempting to write even one value into data (that is, {{data\[0\]}}) is likely to overwrite memory outside of the bounds of the objectobject's bounds.

Compliant Code Example (Storage Allocation)

This compliant solution dynamically allocates storage for struct flexArrayStruct.

...

Wiki Markup
The {{data\[\]}} member of {{flexStruct}} can now be accessed as described in C99 Section 6.7.2.1, paragraph 16.

Noncompliant Code Example (Copying)

This noncompliant code example attempts to copy an instance of a structure containing a flexible array member (struct flexArrayStruct) by assignment.

...

The problem with this noncompliant code example is that when the structure is copied

  • the size of the flexible array member is not considered

...

  • only the first member of the structure, num, is copied

...

Compliant Solution (Copying)

This compliant solution uses memcpy() to properly copy the content of flexStructA into flexStructB.

...

This compliant solutions ensures that the entire structure, including the flexible array member, is copied correctly copied.

Noncompliant Code Example (Function Arguments)

In this noncompliant code example, the flexible array structure is passed directly to a function which that tries to print the array elements.

...

Because C passes the argument by value, a the structure is copied onto the stack. The size of the flexible array member is not considered when the structure is copied, and only the first member of the structure, num, is copied.

Compliant Solution (Function Arguments)

In this compliant solution, the print_array() function accepts a pointer to the structure rather than the structure itself.

Code Block
bgColor#ccccff
void print_array(struct flexArrayStruct *structP) {
  size_t i;

  puts("Array is: ");
  for (i = 0; i < structP->num; i++) {
    printf("%d", structP->data[i]);
  }
  puts("\n");
}

struct flexArrayStruct *structP;
size_t array_size;
size_t i;

/* initialize array_size */

/* space is allocated for the struct */
structP = (struct flexArrayStruct *)malloc(
  sizeof(struct flexArrayStruct) + sizeof(int) * array_size
);
if (structP == NULL) {
  /* Handle malloc failure */
}

structP->num = array_size;

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

print_array(structP);

Risk Assessment

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

flexible Flexible array structs should not be:

  • declared on the stack; they : They should be on the heap.
  • copied via assignment, they : They should be copied using memcpy() or some a similar function.
  • passed as raw args arguments to functions; pass a A pointer to a flexarray struct should be passed to flexArrayStruct intead.

ROSE can detect all of these.

Related Vulnerabilities

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

References

Wiki Markup
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\]
\[[JTC1/SC22/WG14 N791|http://www.open-std.org/jtc1/sc22/wg14/www/docs/n791.htm]\]

...