UNINIT.STACK.ARRAY.PARTIAL.MUST

Partially uninitialized array

The UNINIT.STACK.ARRAY.PARTIAL.MUST checker finds instances in which the memory of a local array is read when the memory hasn't been initialized completely.

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
  int incomplete_init() {
      int a[10];
      int i;
      for (i = 9; i > 0; i--) {
          a[i] = i;
      }
      return a[0];
  }

Klocwork flags line 7, indicating that an element of uninitialized array 'a' with the index equal to 0 is used.