cybersecurity · beginner · ~10 min
Pre-check the mkstemp contract.
Pre-check a mkstemp template before it reaches the syscall: it must end in at least six X characters, or mkstemp may fail or produce predictable names.
Implement int valid_mkstemp_template(const char *t) that returns 1 if t is non-NULL and ends with at least 6 consecutive X characters (a longer run is fine, e.g. /tmp/cplat-XXXXXXXX), and 0 otherwise.
t: a fixed template string the grader provides (NUL-terminated; may be empty or NULL).Returns 1 if the template's last 6 characters are all X, 0 otherwise.
valid_mkstemp_template("/tmp/cplat-XXXXXX") -> 1
valid_mkstemp_template("/tmp/cplat-XXXXXXXX") -> 1 (more than 6 is fine)
valid_mkstemp_template("/tmp/cplat-XXXXX") -> 0 (only 5)
valid_mkstemp_template("/tmp/cplat-XXXXXY") -> 0 (not the last 6)
valid_mkstemp_template("XXXXXX") -> 1
valid_mkstemp_template("") -> 0
valid_mkstemp_template(NULL) -> 0
NULL: return 0.Xs elsewhere don't count.mkstemp mutates its template in place. If the template lacks the trailing 'XXXXXX', the call fails or — on some old glibcs — produces predictable names. A defensive wrapper checks the template first.
A fixed template string (NUL-terminated; may be empty or NULL).
1 if t ends with at least 6 consecutive 'X' characters; otherwise 0.
Pure string scan; any run of >= 6 trailing X is valid.
int valid_mkstemp_template(const char *t) { /* TODO */ (void)t; return 0; }
Requiring EXACTLY 6 X's. Any suffix of >= 6 X's is fine.
Empty string. String shorter than 6 chars. All X's.
O(strlen).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.