Skip to main content

IAR Embedded Workbench for RH850 3.20.x

MISRAC2012-Dir-4.13_e

In this section:
Synopsis

(Advisory) Functions which are designed to provide operations on a resource should be called in an appropriate sequence. A pointer is used after it has been freed. This might cause data corruption or an application crash.

Enabled by default

No

Severity/Certainty

High/Low

highlow.png
Full description

A pointer is used after it has been freed. This check is identical to MISRAC2012-Rule-1.3_p, SEC-BUFFER-use-after-free-some, MEM-use-free-some, CERT-MEM30-C_b.

Coding standards
CERT MEM30-C

Do not access freed memory

MISRA C:2012 Dir-4.13

(Advisory) Functions which are designed to provide operations on a resource should be called in an appropriate sequence

Code examples

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

#include <stdlib.h>

void example(void) {
  int *x;
  x = (int *)malloc(sizeof(int));
  free(x);
  if (rand()) {
    x = (int *)malloc(sizeof(int));
  }
  else {
    /* x not reallocated along this path */
  }
  (*x)++;
}

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

#include <stdlib.h>

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