Versions Compared

Key

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

...

Code Block
bgColor#FFCCCC
void func(size_t s) {
  int vla[s];
  /* ... */
}
/* ... */
func(size);
/* ... */

Compliant Code Solution

Validate size arguments used in VLA declarations. The solution below ensures the size argument, s, used to allocate vla is in a valid range: 1 to a user defined constant.

Code Block
bgColor#ccccff
#define MAX_ARRAY 1024

void func(size_t s) {
   int vla[s];
   /* ... */
}

/* ... */
if (s < MAX_ARRAY && s != 0) {
   func(s);
} 
else {
   /* Handle Error */
}
/* ... */

Implementation Details

Variable length arrays are not supported by Microsoft compilers.

...