Insecure use gets fn High

gets does not consider buffer boundaries and can lead to buffer overflows.

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

Noncompliant example

1#include <stdio.h>
2#include <string.h>
3
4int DST_BUFFER_SIZE = 120;
5
6int insecureUseGetsFnNonCompliant() {
7    char str[DST_BUFFER_SIZE];
8    // Noncompliant: gets is insecure
9    gets(str);
10    printf("%s", str);
11    return 0;
12}

Compliant example

1#include <stdio.h>
2#include <string.h>
3
4int insecureUseGetsFnCompliant() {
5    char buf[10];
6    printf("Enter text: ");
7    // Compliant: Secure funcion used with specifying size limit
8    fgets(buf, sizeof(buf), stdin); 
9    return 0;
10}