Insecure Use strcat fn High

strcat/strncat that can lead to buffer overflow vulnerabilities because it does not affirm the size of the destination array and do not automatically NULL-terminate strings.

Detector ID
c/insecure-use-strcat-fn@v1.0
Category
Common Weakness Enumeration (CWE) external icon
Tags
-

Noncompliant example

1#include <strings.h>
2
3int DST_BUFFER_SIZE = 120;
4
5void insecureUseStrcatNonCompliant(char* src, char* dst) {
6    int n = DST_BUFFER_SIZE;
7    if ((dst != NULL) && (src != NULL) && (strlen(dst)+strlen(src)+1 <= n)) {
8        // Noncompliant: Does not affirm length
9        strcat(dst, src);
10        // Noncompliant: Hardcoded length passed
11        strncat(dst, src, 100);
12    }
13}

Compliant example

1#include <strings.h>
2
3void insecureUseStrcatCompliant(char* src, char* dest, int dest_size) {
4    // Compliant: No hardcoded length
5    strncat(dest, src, dest_size - 1);
6    dest[dest_size - 1] = '\0';
7}