Skip to main content

IAR Embedded Workbench for RL78 5.20

LIB-strcat-overrun-pos

In this section:
Synopsis

A call to strcat might cause destination buffer overrun.

Enabled by default

No

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

A call to the strcat function might cause a destination buffer overrun. This check is identical to CERT-STR31-C_d.

Coding standards
CERT STR31-C

Guarantee that storage for strings has sufficient space for character data and the null terminator

CWE 119

Improper Restriction of Operations within the Bounds of a Memory Buffer

CWE 120

Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')

CWE 121

Stack-based Buffer Overflow

CWE 122

Heap-based Buffer Overflow

CWE 676

Use of Potentially Dangerous Function

Code examples

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

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

void example(void)
{
  char *str1 = "Hello World!\n";
  char *str2 = (char *)malloc(13);
  strcpy(str2,"");
  strcat(str2,str1);
}

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

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

void example(void)
{
  char *str1 = "Hello World!\n";
  char *str2 = (char *)malloc(14);
  strcpy(str2, "");
  strcat(str2, str1);
}