cybersecurity · intermediate · ~15 min · safe pentest lab
Pinned-substring matching with adjacency checks.
Implement:
int count_high_severity(const char *json);
Walk the input one \n-delimited record at a time. For each record
that contains the exact substring "severity":"HIGH", increment
a counter. Return the total, or 0 for NULL.
'{"severity":"HIGH"}\n{"severity":"LOW"}\n' → 1'{"severity":"HIGHEST"}\n' → 0 (must end in ", not EST)'{"name":"HIGH"}\n' → 0 (key is name, not severity)strstr(line, "\"severity\":\"HIGH\"") does most of
the work. Check the byte right after the match is " (or end of
line) — otherwise HIGHEST will match.strstr(line, "HIGH") alone. That gets
fooled by HIGHER, HIGHEST, HIGHWAY, etc.Triaging a CVE feed line-by-line with a pinned substring sweep is the fastest path from feed → alert. The lesson is in being precise about what you match.
A NUL-terminated string of \n-separated mock JSON records.
Count (>= 0). 0 on NULL.
The full needle is "severity":"HIGH" followed by ". Match the closing quote.
int count_high_severity(const char *json) {
/* TODO */
(void)json;
return 0;
}
Trusting strstr("HIGH") alone. Forgetting to advance past the match — infinite loop. Counting "name":"HIGH" because the key wasn't pinned.
Trailing record without newline. Mixed-case (we only accept all-caps HIGH).
O(n) where n is the input length.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.