Skip to main content

IAR Embedded Workbench for RL78 5.20

CERT-MEM30-C_c

In this section:
Synopsis

Do not access freed memory.

Enabled by default

Yes

Severity/Certainty

High/High

highhigh.png
Full description

Evaluating a pointer-including dereferencing the pointer, using it as an operand of an arithmetic operation, type casting it, and using it as the right-hand side of an assignment-into memory that has been deallocated by a memory management function is undefined behavior.

Coding standards
CERT MEM30-C

Do not access freed memory

CWE 416

Use After Free

CWE 456

Missing Initialization

CWE 672

Operation on a Resource after Expiration or Release

CWE 758

Reliance on Undefined, Unspecified, or Implementation-Defined Behavior

Code examples

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

#include <stdlib.h>

struct node {
    int value;
    struct node *next;
};

void free_list(struct node *head) {
    for (struct node *p = head; p != NULL; p = p->next) {
        free(p);
    }
}

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

#include <stdlib.h>

struct node {
    int value;
    struct node *next;
};

void free_list(struct node *head) {
    struct node *q;
    for (struct node *p = head; p != NULL; p = q) {
        q = p->next;
        free(p);
    }
}