Rules / C/C++
SHIELD-CPP-004
Use of gets is inherently unsafe
What it detects
gets performs an unbounded read from stdin and always risks buffer overflow.
How to fix
Replace gets with fgets and a fixed buffer size.
Vulnerable — Shield flags thisinput.c
#include <stdio.h>
int main(void) {
char name[64];
gets(name);
printf("Hello, %s\n", name);
return 0;
}
Fixed — scans cleaninput.c
#include <stdio.h>
int main(void) {
char name[64];
if (fgets(name, sizeof(name), stdin) != NULL)
printf("Hello, %s\n", name);
return 0;
}
Both snippets are verified against the shipped scanner: the vulnerable one triggers SHIELD-CPP-004, the fixed one does not.