Skip to main content

IAR Embedded Workbench for Arm 9.70.x

MEM-malloc-diff-type

In this section:
Synopsis

An allocation call tries to allocate memory based on a sizeof operator, but the destination type of the call is of a different type.

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

This might be an error, and will result in an allocated memory chunk that does not match the destination pointer or array. This might easily result in an invalid memory dereference, and crash the application.

Coding standards
CERT MEM35-C

Allocate sufficient memory for an object

CWE 131

Incorrect Calculation of Buffer Size

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>

int* foo(){
  return malloc(sizeof(char)*10);
}

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

#include <stdlib.h>

char* foo(){
  return malloc(sizeof(char)*10);
}