Pointer deallocation without a corresponding dynamic allocation
This defect occurs
when a block of memory released using the free function
was not previously allocated using malloc, calloc,
or realloc.
The free function releases a block of memory allocated on the
heap. If you try to access a location on the heap that you did not allocate
previously, a segmentation fault can occur.
The issue can highlight coding errors. For instance, you perhaps wanted to use the
free function or a previous malloc
function on a different pointer.
In most cases, you can fix the issue by removing the free
statement. If the pointer is not allocated memory from the heap with
malloc or calloc, you do not need to free
the pointer. You can simply reuse the pointer as required.
If the issue highlights a coding error such as use of free or
malloc on the wrong pointer, correct the error.
If the issue occurs because you use the free function to free
memory allocated with the new operator, replace the
free function with the delete
operator.
| Group: Dynamic Memory |
| Language: C | C++ |
| Default: On |
Command-Line Syntax: BAD_FREE |
| Impact: High |
| CWE ID: 404, 590, 762 |