cybersecurity · intermediate · ~15 min · safe pentest lab
Adopt allowlist validation as a habit.
Accept a string only if every character is on an approved allowlist and its length is in range — the safe alternative to blocking known-bad characters.
Implement int validate_user_input(const char *s) that returns 0 if s is valid and -1 otherwise.
s is valid when all of these hold:
- _ . @ +.A NUL-terminated string s the grader passes. s may be NULL.
Returns 0 if s is valid, -1 otherwise.
validate_user_input("alice99@example.com") -> 0
validate_user_input("alice.smith") -> 0
validate_user_input("ali ce") -> -1 (space not allowed)
validate_user_input("alice;") -> -1 (';' not allowed)
validate_user_input("") -> -1 (too short)
validate_user_input(NULL) -> -1
NULL pointer: return -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.
A NUL-terminated string s (may be NULL).
0 if s is 1..64 chars and only letters/digits/-_.@+; otherwise -1.
Allowlist the permitted characters; reject everything else.
#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.