Skip to main content

IAR Embedded Workbench for RISC-V 3.40

MEM-return-free

In this section:
Synopsis

A function deallocates memory, then returns a pointer to that memory.

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

A function deallocates memory, then returns a pointer to that memory. If the callee of this function attempts to dereference the returned pointer, this will cause a runtime error.

Coding standards
CWE 416

Use After Free

Code examples

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

#include <stdlib.h>

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

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

#include <stdlib.h>

int *example(void) {
  int *a = malloc(sizeof(int));
  return a;
}