Skip to main content

IAR Embedded Workbench for RISC-V 3.40

PTR-null-literal-pos

In this section:
Synopsis

A literal pointer expression (like NULL) is dereferenced by a function call.

Enabled by default

No

Severity/Certainty

High/Medium

highmedium.png
Full description

A literal pointer expression (for example NULL) is passed as argument to a function that might dereference it. Pointer values are generally only useful if acquired at runtime, and thus dereferencing a literal address is usually unintentional, resulting in corrupted memory or an application crash.

Coding standards
CWE 476

NULL Pointer Dereference

Code examples

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

#define NULL ((void *) 0)

extern int sometimes;

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

int foo(int *x) {
  bar(NULL);
}

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);
}