cybersecurity · intermediate · ~15 min · safe pentest lab
Adopt allowlist validation as a habit.
Implement int validate_user_input(const char *s) returning 0 if s is between 1 and 64 chars and contains only ASCII letters, digits, or one of -_.@+; otherwise -1.
The most common input bug isn't malicious content — it's malformed length. A function that asserts input length is in a known range catches 90% of bugs before they reach business logic.
#include <stdio.h>
#include <ctype.h>
int validate_user_input(const char *s) {
/* TODO */
return -1;
}
Off-by-one on the upper bound. Not checking for NULL. Using strlen on non-NUL-terminated input.
Empty input. Input exactly at the limit. Input one byte over the limit. NULL pointer.
O(length).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.