In this section:
This error is generated if the address of a global variable is passed to a routine that de-allocates memory.
|
The following code attempts to deallocate a global variable that was not dynamically allocated.
/* * File: freeglob.c */ char a[10]; main() { free(a); return (0); } |
[freeglob.c:8] **FREE_GLOBAL** >> free(a); Freeing global memory: a ---- Associated Common Weakness Enumerations ---- CWE-763: Release of invalid pointer or reference CWE-590: Free of memory not on the heap Pointer : 0x00012210 In block : 0x00012210 thru 0x00012217 (8 bytes) a,declared at freeglob.c, 4 Stack trace where the error occurred: main() freeglob.c, 8 **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 some cases, this error will result from a simple coding mistake at the indicated source line which can be quickly corrected.
A more complex problem may arise when a program uses both statically and dynamically allocated blocks in the same way. A common example is a linked list in which the head of the list is static, while the other entries are allocated dynamically. In this case, you must take care not to free the static list head when removing entries.
If your application is unable to distinguish between global and dynamically allocated memory blocks, you can suppress error messages by suppressing FREE_GLOBAL
.
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 |