Skip to main content

IAR Embedded Workbench for Arm 9.70.x

CERT-EXP34-C_b

In this section:
Synopsis

Do not dereference null pointers.

Enabled by default

Yes

Severity/Certainty

High/High

highhigh.png
Full description

Dereferencing a null pointer is undefined behavior. On many platforms, dereferencing a null pointer results in abnormal program termination, but this is not required by the standard. This check is identical to PTR-null-assign-fun-pos.

Coding standards
CERT EXP34-C

Do not dereference null pointers

Code examples

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

#define NULL ((void*)  0)
void * malloc(unsigned long);

int * xmalloc(int size){
  int * res = malloc(sizeof(int)*size);
  if (res != NULL)
    return res;
  else 
    return NULL;
}

void zeroout(int *xp, int i)
{
  xp[i] = 0;
}

int foo() {
  int * x;
  int i;
  x = xmalloc(45);  
  // if (x)
  //  return -1;  
  for(i = 0; i < 45; i++)
    zeroout(x, i);

}

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

#define NULL ((void*)  0)
void * malloc(unsigned long);

int * xmalloc(int size){
  int * res = malloc(sizeof(int)*size);
  if (res != NULL)
    return res;
  else 
    return NULL;
}

void zeroout(int *xp, int i)
{
  xp[i] = 0;
}

int foo() {
  int * x;
  int i;
  x = xmalloc(45);  
  if (x == NULL)
    return -1;
  else {
    for(i = 0; i < 45; i++)
      zeroout(x, i);
  }
}