Skip to main content

IAR Embedded Workbench for RH850 3.20.x

MISRAC2012-Rule-22.2_b

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

Medium/Medium

mediummedium.png
Full description

Freeing a memory location more than once on some paths but not others. This check is identical to MEM-double-free-some.

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 example(void) {
    int *ptr = (int*)malloc(sizeof(int));
    free(ptr);
    if(rand() % 2 == 0)
    {
      free(ptr);
    }
}

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

#include <stdlib.h>
void example(void) {
    int *ptr = (int*)malloc(sizeof(int));
    if(rand() % 2 == 0)
    {
      free(ptr);
    }
    else
    {
      free(ptr);
    }
}