Skip to main content

IAR Embedded Workbench for RX 5.20

PTR-null-cmp-bef

In this section:
Synopsis

A pointer is compared with NULL, then dereferenced.

Enabled by default

Yes

Severity/Certainty

High/Low

highlow.png
Full description

A pointer is compared with NULL, then dereferenced. This might occur if the wrong comparison operator is used, for example if == instead of !=, or if the then- and else- clauses of an if-statement are accidentally swapped. If the condition is evaluated and found to be true, the application will crash. This check is identical to CERT-EXP34-C_g.

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 example(void) {
  int *p;
  if (p == NULL) {
    *p = 4;  //dereference after comparison with NULL
  }
  return 1;
}

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

#include <stdlib.h>

int example(void) {
  int *p;
  if (p != NULL) {
    *p = 4;  //OK - after comparison with non-NULL
  }
  return 1;
}