Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

UNINIT.HEAP.MIGHT

Uninitialized heap use possible

The UNINIT.HEAP.MIGHT checker looks for heap memory allocated with malloc that may not have been initialized before it's used.

Vulnerability and risk

Using uninitialized heap memory can result in significant performance irregularities for your code, as the values assigned to an given data object are randomly picked from the heap memory allocated and could reflect the state of previously-used objects or objects from another process. If the software doesn't initialize memory correctly, unexpected results can occur, possibly with security implications.

Mitigation and prevention

Heap memory is always uninitialized on return from the popular allocation function malloc(). To avoid uninitialized memory problems, make sure all variables and resources are initialized explicitly before their first usage.

Another alternative is to use the standard calloc() library function, which automatically initializes all allocated memory to zero.

Vulnerable code example

1  struct s {
2    int i;
3  };
4  
5  int f(struct s **v, int t) {
6    *v = (struct s *)malloc(sizeof(struct s));
7    if (t > 0) {
8      (*v)->i = t;
9    }
10   return (*v)->i;
11 }

Klocwork flags line 10, indicating that variable '(*v)->i', which gets its value from the memory allocated at line 5, might be used uninitialized at line 10.

Related checkers