Skip to main content

IAR Embedded Workbench for RX 5.20

PTR-singleton-arith-pos

In this section:
Synopsis

Pointer arithmetic might be performed on a pointer that points to a single object.

Enabled by default

No

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

Pointer arithmetic might be performed on a pointer that points to a single object. If this pointer is subsequently dereferenced, it could be pointing to invalid memory, causing a runtime error.

Coding standards
CWE 119

Improper Restriction of Operations within the Bounds of a Memory Buffer

Code examples

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

#include <stdlib.h>
void example(int a) {
  int *p;
  if (a) {
    p = malloc(sizeof(int) * 10);
  } else {
    p = malloc(sizeof(int));
  
  }
  p = p + 1;
}

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

#include <stdlib.h>
void example(int a) {
  int *p;
  if (a) {
    p = malloc(sizeof(int) * 10);
  } else {
    p = malloc(sizeof(int) * 20);
  
  }
  p = p + 1;
}