Skip to main content

IAR Embedded Workbench for RH850 3.20.x

MISRAC2012-Rule-21.17_e

In this section:
Synopsis

(Mandatory) Use of the string handling functions from <string.h> shall not result in accesses beyond the bounds of the objects referenced by their pointer parameters.

Enabled by default

Yes

Severity/Certainty

High/Medium

highmedium.png
Full description

A call to strncmp might cause a buffer overrun.

Coding standards
MISRA C:2012 Rule-21.17

(Mandatory) Use of the string handling functions from <string.h> shall not result in accesses beyond the bounds of the objects referenced by their pointer parameters

Code examples

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

#include <stdlib.h>
#include <string.h>

void example(int d) {
  char *a = malloc(sizeof(char) * 10);
  char *b = malloc(sizeof(char) * 10);
  int c;
  if (d) {
    c = 20;
  } else {
    c = 5;
  }
  strncmp(a, b, c);
}

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

#include <stdlib.h>
#include <string.h>

void example(int d) {
  char *a = malloc(sizeof(char) * 10);
  char *b = malloc(sizeof(char) * 10);
  int c;
  if (d) {
    c = 8;
  } else {
    c = 5;
  }
  strncmp(a, b, c);
}