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

1  int incomplete_init() {
2      int a[10];
3      int i;
4      for (i = 9; i > 0; i--) {
5          a[i] = i;
6      }
7      return a[0];
8  }

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