cybersecurity · intermediate · ~15 min · safe pentest lab
Tally several dangerous patterns.
Tally the classic unbounded-copy calls in a source snippet.
Implement int count_unsafe(const char *code) that returns the total number of occurrences of "strcpy(", "strcat(", "sprintf(", and "gets(" (summed across all four).
code: a NUL-terminated string of C source text the grader passes.Returns int: the combined count of all four unsafe-call substrings.
count_unsafe("strcpy(a,b); x; sprintf(c,\"%d\",n);") -> 2
count_unsafe("snprintf(c,n,fmt);") -> 0
snprintf( does not contain sprintf(, so a safe snprintf call is not counted.A NUL-terminated string code of C source text.
An int: the summed count of the four unsafe-call substrings.
Count strcpy(, strcat(, sprintf(, gets(; advance past each hit.
#include <string.h>
int count_unsafe(const char *code) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.