In this section:

Overview

This error is generated whenever a function returns a pointer to a (non-static) local variable. Since the stack frame of this routine will disappear when the function returns, this pointer is never valid.

In order for Insure++ to find this error, the suppressEDGWarning off option is required.

CodeDescriptionEnabledReportedPlatform
RETURN_DANGLINGReturning pointer to local variable(tick)CompilationWindows/Unix


Problem

The following code shows the routine foo returning a pointer to a local variable.

/*
 * File: retdngl.c
 */
char *foo()
{
	char b[10];
	return b;
}

main()
{
	char *a = foo();
	return 0;
}

Diagnosis During Compilation

[retdngl.c:7] **RETURN_DANGLING**
	Returning pointer to local variable: b.
>>		 return b;

---- Associated Common Weakness Enumerations ----
CWE-119: Improper restriction of operations within a memory buffer
CWE-562: Return of stack variable address

Repair

The pointer returned in this manner can be made legal in one of several ways:

Occasionally, the value returned from the function is never used in which case it is safest to change the declaration of the routine to indicate that no value is returned.

References

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

CWEDescription
CWE-119Improper restriction of operations within a memory buffer
CWE-562Return of stack variable address