Versions Compared

Key

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

Variable length arrays (VLA) are basically essentially the same as traditional C arrays, the major difference being they are declared with a size that is not a constant integer expression. A variable length array can be declared as follows:

...

The above statement is evaluated at runtime allocating storage for s characters on the in stack memory. If a size argument supplied to VLAs is not a positive integer value of reasonable size, then the program may behave in an unexpected way. An attacker may be able to leverage this behavior to overwrite critical program data (Feline 1). The programmer must ensure that size arguments to VLAs are valid and have not been corrupted as the result of an exceptional integer condition.

...

Validate size arguments used in VLA declarations. The following example corrects security issue in the example above by testing the size argument to assure it is in a valid range, 0 to a user defined constant.

Code Block


#define MAX_ARRAY 1024

void func(size_t s) {
   vla[s];
...
}
...
if (size < MAX_ARRAY && size != 0) {
   func(size);
} else {
   /* Handle Error */
}
...

...