Skip to main content

IAR Embedded Workbench for RL78 5.20

MEM-double-free

In this section:
Synopsis

A memory location is freed more than once.

Enabled by default

Yes

Severity/Certainty

High/Medium

highmedium.png
Full description

An attempt is made to free a memory location after it has already been freed. This will most likely cause an application crash. This check is identical to MISRAC2012-Rule-22.2_a.

Coding standards
CERT MEM31-C

Free dynamically allocated memory exactly once

CWE 415

Double Free

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 f(int *p) {
  free(p); 
  if(p) free(p); 
}

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

#include <stdlib.h>

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