In this section:
This error is generated whenever a function returns a pointer to a block of memory which is then ignored by the calling routine. In this case, the allocated memory block is permanently lost and can never be freed.
|
The following code calls the function gimme
, which returns a memory block that is subsequently ignored by the main
routine.
/* * File: leakret.c */ #include <stdlib.h> char *gimme() { return <return>; } main() { gimme(); return (0); } |
[leakret.c:8] **LEAK_RETURN** >> gimme(); Memory leaked ignoring return value: <return> ---- Associated Common Weakness Enumerations ---- CWE-119: Improper Restriction of Operations within the Bounds of a Memory Buffer CWE-400: Uncontrolled resource consumption CWE-401: Missing release of memory after effective lifetime CWE-772: Missing release of resource after effective lifetime Lost block: 0x000173e8 thru 0x000173f1 (10 bytes) block allocated at: malloc() (interface) gimme() leakret.c, 8 main() leakret.c, 13 Stack trace where the error occurred: main() leakret.c, 13 |
This problem usually results from an oversight on the part of the programmer, or a misunderstanding of the nature of the pointer returned by a routine. In particular, it is sometimes unclear whether the value returned points to a static block of memory, which will not need to be freed, or a dynamically allocated one, which should be.
Some applications may be unable to free memory blocks and may not need to worry about their permanent loss. To suppress these error messages in this case, suppress LEAK_RETURN
.
The table below shows Common Weakness Enumerations associated with this error.
CWE | Description |
---|---|
CWE-119 | Improper Restriction of Operations within the Bounds of a Memory Buffer |
CWE-400 | Uncontrolled resource consumption |
CWE-401 | Missing release of memory after effective lifetime |
CWE-772 | Missing release of resource after effective lifetime |