Calling free() on memory not allocated by malloc/calloc/realloc

ID

c.memory_management.incorrect_use_of_free

Severity

high

Resource

Memory Management

Language

C / C++

Description

The software calls free() on a pointer to memory that has a short lifetime and was not allocated using associated heap allocation functions such as malloc(), calloc(), or realloc().

Rationale

The software calls free() on a pointer to memory that has a short lifetime and was not allocated using associated heap allocation functions such as malloc(), calloc(), or realloc().

The following code illustrates a vulnerable pattern detected by this rule:

void wrong()
{
	record_t bar[MAX_SIZE];

	/* do something interesting with bar */

	// VULNERABLE: Calling free() on memory not allocated by malloc/calloc/realloc
	free(bar);
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.