Skip to main content

IAR Embedded Workbench for Arm 9.70.x

PTR-null-assign

In this section:
Synopsis

A pointer is assigned the value NULL, then dereferenced.

Enabled by default

Yes

Severity/Certainty

High/High

highhigh.png
Full description

A pointer is assigned the value NULL, then dereferenced. Assigning the pointer the value NULL might have been intentional to indicate that the pointer is no longer being used, but it is an error to subsequently dereference it, and will cause an application crash. This check is identical to CERT-EXP34-C_d.

Coding standards
CERT EXP34-C

Do not dereference null pointers

CWE 476

NULL Pointer Dereference

Code examples

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

#include <stdlib.h>

int main(void) {
  int *p;
  p = NULL;
  return *p;  //dereference after
              //assignment to NULL
}

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

#include <stdlib.h>

int main(void) {
  int *p;
  p = NULL;
  p = (int *)1;
  return *p;
}