CWE-590¶
Free of Memory not on the Heap. [Improper-Control-Of-A-Resource-Through-Its-Lifetime]
Required inputs: IR, StaticSemanticAnalysis
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¶
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
resources¶
resources
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.Type: set[str]
Default:
{'C++ArrayHeapMemory', 'C++HeapMemory', 'CudaAsyncMemory', 'CudaDeviceMemory', 'CudaDriverAsyncMemory', 'CudaHostMemory', 'CudaManagedMemory', 'HeapMemory', 'UniquePtrHeapMemory'}
witness_paths¶
witness_paths : bool = True