MISRAC2012-Rule-21.17_d
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

Full description
A call to strncat might cause a destination 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 <string.h>
#include <stdlib.h>
void example(int d) {
char * a = malloc(sizeof(char) * 5);
char * b = malloc(sizeof(char) * 100);
int c;
if (d) {
c = 10;
} else {
c = 5;
}
strcpy(a, "0123");
strcpy(b, "45678901234");
strncat(a, b, c);
}
The following code example passes the check and will not give a warning about this issue:
#include <string.h>
#include <stdlib.h>
void example(int d) {
char * a = malloc(sizeof(char) * 5);
char * b = malloc(sizeof(char) * 100);
int c;
if (d) {
c = 2;
} else {
c = 3;
}
strcpy(a, "0123");
strcpy(b, "45678901234");
strncat(b, a, c);
}