cybersecurity · intermediate · ~15 min · safe pentest lab

Extract the CVE year

Parse a fixed-position field.

Challenge

Pull the disclosure year out of a CVE id so vulnerabilities can be bucketed by age.

Task

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.

Input

  • s: a NUL-terminated CVE id string the grader passes (e.g. CVE-2021-44228).

Output

Returns int: the year as a number (e.g. 2021), or -1 if the CVE-YYYY prefix is malformed.

Example

cve_year("CVE-2021-44228")   ->   2021
cve_year("CVE-xx-1")         ->   -1

Edge cases

  • Only the CVE- prefix and the 4 year digits are validated here.

Input format

A NUL-terminated CVE id string s.

Output format

An int: the 4-digit year, or -1 if the CVE-YYYY prefix is bad.

Constraints

Require CVE- then 4 digits; otherwise return -1.

Starter code

#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.