Skip to main content

IAR Embedded Workbench for RISC-V 3.40

MEM-malloc-arith

In this section:
Synopsis

An assignment contains both a malloc() and pointer arithmetic on the right-hand side.

Enabled by default

No

Severity/Certainty

High/Medium

highmedium.png
Full description

An assignment contains both a malloc() and pointer arithmetic on the right-hand side. If this is unintentional, the start of the allocated memory block might be lost, and a buffer overflow is possible.

Coding standards

This check does not correspond to any coding standard rules.

Code examples

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

#include <stdlib.h>

int example(void) {
  int *p;

  p = (int *)malloc(255) + 10;  //pointer arithmetic

  return 0;
}

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

#include <stdlib.h>

int example(void) {
  int *p;

  p = (int *)malloc(255);

  return 0;
}