In this section:

Overview

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.

CodeDescriptionEnabledReportedPlatform
LEAK_RETURN

Memory leaked by ignoring returned value

(tick)RuntimeWindows/Unix


Problem

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);
}

Diagnosis at Runtime

[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
  • Line 2: Source file and line at which the problem was detected.
  • Line 3: Description of the problem and the block that is to be lost.
  • Line 5-9: CWEs associated with this problem.
  • Line 11: Description of the block of memory that is about to be lost, including its size and the line number at which it was allocated.
  • Line 16: Stack trace showing the function call sequence leading to the error.

Repair

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.

References

The table below shows Common Weakness Enumerations associated with this error.

CWEDescription
CWE-119Improper Restriction of Operations within the Bounds of a Memory Buffer
CWE-400Uncontrolled resource consumption
CWE-401Missing release of memory after effective lifetime
CWE-772Missing release of resource after effective lifetime
  • No labels