CWE-590

Free of Memory not on the Heap. [Improper-Control-Of-A-Resource-Through-Its-Lifetime]

Required inputs: IR, StaticSemanticAnalysis

The product calls free() on a pointer to memory that was not allocated using associated heap allocation functions such as malloc(), calloc(), or realloc(). When free() is called on an invalid pointer, the program's memory management data structures may become corrupted. This corruption can cause the program to crash or, in some circumstances, an attacker may be able to cause free() to operate on controllable memory locations to modify critical program variables or execute code.
Demonstrative Examples
Example 1

In this example, an array of record_t structs, bar, is allocated automatically on the stack as a local variable and the programmer attempts to call free() on the array. The consequences will vary based on the implementation of free(), but it will not succeed in deallocating the memory.

Example Language:C
    void foo(){
        record_t bar[MAX_SIZE];

        /* do something interesting with bar */

        ...
        free(bar);
    }

This example shows the array allocated globally, as part of the data segment of memory and the programmer attempts to call free() on the array.

Example Language:C
    record_t bar[MAX_SIZE]; //Global var
    void foo(){
        /* do something interesting with bar */
        ...
        free(bar);
    }

Instead, if the programmer wanted to dynamically manage the memory, malloc() or calloc() should have been used.

void foo(){
        record_t *bar = (record_t*)malloc(MAX_SIZE*sizeof(record_t));

        /* do something interesting with bar */

        ...
        free(bar);
    }

Additionally, you can pass global variables to free() when they are pointers to dynamically allocated memory.

record_t *bar; //Global var
    void foo(){
        bar = (record_t*)malloc(MAX_SIZE*sizeof(record_t));

        /* do something interesting with bar */

        ...
        free(bar);
    }
Excerpts from CWE [https://cwe.mitre.org], Copyright (C) 2006-2026, the MITRE Corporation. See section 9.4. "3rd-Party Licenses" in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

possible_stack_free

{name0} possibly released by call to {node0} is a stack or static object

None

False

stack_free

{name0} released by call to {node0} is a stack or static object

None

False

Options

resources

resources

Type: set[str]

Default: {'C++ArrayHeapMemory', 'C++HeapMemory', 'CudaAsyncMemory', 'CudaDeviceMemory', 'CudaDriverAsyncMemory', 'CudaHostMemory', 'CudaManagedMemory', 'HeapMemory', 'UniquePtrHeapMemory'}

Deallocator calls of these resources are checked for being called with a stack/static object; the names are a selection of rules in the Resources group.
 

witness_paths

witness_paths : bool = True

Whether witness paths should be determined and included in the issue.