UNINIT.STACK.ARRAY.MIGHT

Uninitialized array possible

The UNINIT.STACK.ARRAY.MIGHT checker finds instances in which the memory of a local array is read when the memory may not have been initialized.

Vulnerability and risk

In C++, stack variables aren't initialized by default. They generally contain random junk data from the current content of stack memory. The uninitialized data may contain values that cause program flow to change in unintended ways, possibly with security implications.

Mitigation and prevention

To avoid initialization problems, make sure all variables and resources are initialized explicitly before their first usage. Take note of complex conditional situations and make sure that all paths include the initialization.

Vulnerable code example

Copy
  extern void pfoo(int *);
  extern int some_condition();
  
  void uninit_array_might() {
      int *a[10];
      if (some_condition()) {
          int i;
          for(i = 0; i < 10; i++) {
              a[i] = 0;
         }
     }
     pfoo(a[4]);
 }

Klocwork flags line 12, indicating that the memory of array 'a' can be used when the array might be uninitialized.