In this section:

Overview

This error is generated when a memory block is freed multiple times.

CodeDescriptionEnabledReportedPlatform
FREE_DANGLING

Freeing dangling pointer

(tick)RuntimeWindows/Unix


Problem

The following code frees the same pointer twice.

/*
 * File: freedngl.c
 */
#include <stdlib.h>

main()
{
	char *a = (char *)malloc(10);
	free(a);
	free(a);
	return (0);
}

Diagnosis at Runtime

[freedngl.c:10] **FREE_DANGLING**
>> 			free(a);
Freeing dangling pointer: a

---- Associated Common Weakness Enumerations ----
CWE-119: Improper Restriction of Operations within the Bounds of a Memory Buffer
CWE-416: Use after free
CWE-825: Expired pointer dereference

Pointer			 : 0x000173e0
In block		 : 0x000173e0 thru 0x000173e9 (10 bytes)
					block allocated at:
						malloc() (interface)
							main() freedngl.c, 8
				stack trace where memory was freed:
						main() freedngl.c, 9
Stack trace where the error occurred:
				main() freedngl.c, 10
**Memory corrupted. Program may crash!!**
  • Line 2: Source line at which the problem was detected.
  • Line 3: Description of the problem and the expression that is in error.
  • Line 5-8: CWEs associated with this problem.
  • Line 10: Value of the pointer that is being deallocated.
  • Line 11: Information about the block of memory addressed by this pointer, including information about where this block was allocated.
  • Line 15: Stack trace showing where this block was freed.
  • Line 17: Stack trace showing the function call sequence leading to the error.
  • Line 19: Informational message indicating that a serious error has occurred which may cause the program to crash.

Repair

Some systems allow memory blocks to be freed multiple times. However, this is not portable and is not a recommended practice.

The information supplied in the diagnostics will allow you to see the line of code which previously deallocated this block of memory. You should attempt to remove one of the two calls.

If your application is unable to prevent multiple calls to deallocate the same block, you can suppress error messages by suppressing FREE_DANGLING.

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-416Use after free
CWE-825Expired pointer dereference
  • No labels