cybersecurity · intermediate · ~15 min · safe pentest lab
Parse a fixed-position field.
Pull the disclosure year out of a CVE id so vulnerabilities can be bucketed by age.
Implement int cve_year(const char *s) that returns the 4-digit year from a CVE id, or -1 if s does not start with CVE- followed by 4 digits.
s: a NUL-terminated CVE id string the grader passes (e.g. CVE-2021-44228).Returns int: the year as a number (e.g. 2021), or -1 if the CVE-YYYY prefix is malformed.
cve_year("CVE-2021-44228") -> 2021
cve_year("CVE-xx-1") -> -1
CVE- prefix and the 4 year digits are validated here.A NUL-terminated CVE id string s.
An int: the 4-digit year, or -1 if the CVE-YYYY prefix is bad.
Require CVE- then 4 digits; otherwise return -1.
#include <string.h>
int cve_year(const char *s) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.