Skip to main content

IAR Embedded Workbench for RISC-V 3.40

PTR-null-cmp-bef-fun

In this section:
Synopsis

A pointer is compared with NULL, then dereferenced by a function.

Enabled by default

Yes

Severity/Certainty

High/Low

highlow.png
Full description

A pointer is compared with NULL, then passed as an argument to a function that might dereference it. 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 function does dereference the pointer, the application will crash. If it does not, the argument is unneeded. This check is identical to CERT-EXP34-C_f.

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:

#define NULL ((void *) 0)

int bar(int *x){
  *x = 3;
  return 0;
}

int foo(int *x) {
  if (x != NULL) {    
    *x = 4;
  }
  bar(x);
}

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

#define NULL ((void *) 0)

int bar(int *x){
  if (x != NULL)
    *x = 3;
  return 0;
}

int foo(int *x) {
  if (x != NULL) {    
    *x = 4;
  }
  bar(x);
}