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

FUM.GEN.MUST

Freeing unallocated memory

If unallocated memory is freed, results can be unpredictable. The FUM.GEN.MUST checker finds deallocation functions that are invoked with an uninitialized pointer.

Vulnerability and risk

When free() is called on an invalid pointer, it can cause corruption of a program's memory, resulting in program crash, and possibly creating a vulnerability that an attacker can exploit.

Mitigation and prevention

To avoid freeing non-heap memory, make sure that you:

  • free only pointers that were allocated on the heap with malloc() previously
  • keep track of pointers and free them only once
  • free only memory that belongs to the right part of the program

Vulnerable code example

1      typedef struct list {
2        char * next;
3      } list;
4      typedef struct z {
5        list l;
6      } z;
7  
8      void a6(){ // 8923
9        z * x = malloc(sizeof(z));
10       if (!x) return;
11       x->l.next=1;
12       free(x->l.next);
13     }

Klocwork produces an issue report at line 12 indicating that unallocated memory referenced by 'x->l.next' and assigned on line 11 was freed at line 12. Freeing unallocated memory can cause an application to crash, and possibly result in vulnerability to attack.

Extension

This checker can be extended through the Klocwork knowledge base. See Tuning C/C++ analysis for more information.