Skip to main content

IAR Embedded Workbench for RH850 3.20.x

MEM-free-variable

In this section:
Synopsis

A stack address might be freed.

Enabled by default

Yes

Severity/Certainty

High/High

highhigh.png
Full description

A stack address might be freed. Stack variables are automatically deallocated when they go out of scope. Consequently, explicitly freeing them might cause a crash or corrupt the surrounding stack data. Erroneously using free() on stack memory might also corrupt stdlib's memory bookkeeping, affecting heap memory. This check is identical to MISRAC2012-Rule-22.2_c, CERT-MEM34-C_a.

Coding standards
CERT MEM34-C

Only free memory allocated dynamically

CWE 590

Free of Memory not on the Heap

MISRA C:2012 Rule-22.2

(Mandatory) A block of memory shall only be freed if it was allocated by means of a Standard Library function

Code examples

The following code example fails the check and will give a warning:

#include <stdlib.h>
void example(void){
  int x=0;
  free(&x);
}

The following code example passes the check and will not give a warning about this issue:

void example(void) {
  int *p;
  p = (int *)malloc(sizeof( int));
  free(p);
}