Skip to main content

IAR Embedded Workbench for RX 5.20

SEC-NULL-cmp-aft

In this section:
Synopsis

A pointer is dereferenced, then compared with NULL.

Enabled by default

Yes

Severity/Certainty

High/Medium

highmedium.png
Full description

Checks whether a dereferenced pointer are subsequently compared with NULL. Dereferencing a pointer implicitly asserts that it is not NULL. Comparing it with NULL after this may suggests that it may have been NULL at the point of dereference. The pointer should be checked to be non-NULL before being derefenced.

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;
  *p = 4;  //line 8 asserts that p may be NULL  
  if (p != NULL) {
    return 0;
  }
  return 1;
}

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

#include <stdlib.h>

void example(int *p) {
  if (p == NULL) {
    return;
  }
  *p = 4;
}