In this section:
This error is generated if the address of a local variable is passed to free
.
|
The following code attempts to free a local variable that was not dynamically allocated.
/* * File: freelocl.c */ main() { char b, *a; a = &b; free(a); return (0); } |
[freelocl.c:9] **FREE_LOCAL** >> free(a); Freeing local memory: a ---- Associated Common Weakness Enumerations ---- CWE-763: Release of invalid pointer or reference CWE-590: Free of memory not on the heap Pointer : 0xf7fffb0f In block : 0xf7fffb0f thru 0xf7fffb0f (1 byte) b,declared at freelocl.c, 6 Stack trace where the error occurred: main() freelocl.c, 9 **Memory corrupted. Program may crash!!** |
Some systems allow this operation since they keep track of which blocks of memory are actually dynamically allocated, but this is not portable programming practice and is not recommended.
In most cases, this error will result from a simple coding mistake at the indicated source line which can be quickly corrected.
If your application is unable to distinguish between local variables and dynamically allocated memory blocks, you can suppress error messages by suppressing FREE_LOCAL
.
The table below shows Common Weakness Enumerations associated with this error.
CWE | Description |
---|---|
CWE-763 | Release of invalid pointer or reference |
CWE-590 | Free of memory not on the heap |