Skip to main content

IAR Embedded Workbench for RX 5.20

MEM-realloc-diff-type

In this section:
Synopsis

The type of the pointer that stores the result of realloc does not match the type of the first argument.

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

The type of the pointer that stores the result of realloc does not match the type of the first argument. Subsequent accesses to this memory might be misaligned and cause a runtime error. This check is identical to CERT-MEM35-C_c.

Coding standards
CERT MEM35-C

Allocate sufficient memory for an object

CWE 131

Incorrect Calculation of Buffer Size

Code examples

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

#include <stdlib.h>

void example(int *a, int new_size) {
  unsigned int *b;
  b = realloc(a, sizeof(int) * new_size);
}

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

#include <stdlib.h>

void example(int *a, int new_size) {
  int *b;
  b = realloc(a, sizeof(int) * new_size);
}