Skip to main content

IAR Embedded Workbench for RL78 5.20

MISRAC2012-Rule-22.2_a

In this section:
Synopsis

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

Enabled by default

Yes

Severity/Certainty

High/Medium

highmedium.png
Full description

A memory location is freed more than once. This check is identical to MEM-double-free.

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);
}