gets does not consider buffer boundaries and can lead to buffer overflows.
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}
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}