cybersecurity · beginner · ~10 min

Validate that a mkstemp template ends with at least 6 X's

Pre-check the mkstemp contract.

Challenge

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.

Task

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.

Input

  • t: a fixed template string the grader provides (NUL-terminated; may be empty or NULL).

Output

Returns 1 if the template's last 6 characters are all X, 0 otherwise.

Example

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

Edge cases

  • String shorter than 6 characters: return 0.
  • Empty string or NULL: return 0.
  • Only the final 6 characters matter — Xs elsewhere don't count.

Why this matters

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.

Input format

A fixed template string (NUL-terminated; may be empty or NULL).

Output format

1 if t ends with at least 6 consecutive 'X' characters; otherwise 0.

Constraints

Pure string scan; any run of >= 6 trailing X is valid.

Starter code

int valid_mkstemp_template(const char *t) { /* TODO */ (void)t; return 0; }

Common mistakes

Requiring EXACTLY 6 X's. Any suffix of >= 6 X's is fine.

Edge cases to handle

Empty string. String shorter than 6 chars. All X's.

Complexity

O(strlen).

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.