ESBMC
Efficient SMT-based Context-Bounded Model Checker
Loading...
Searching...
No Matches
cwe_mapping.h
Go to the documentation of this file.
1#ifndef CPROVER_UTIL_CWE_MAPPING_H
2#define CPROVER_UTIL_CWE_MAPPING_H
3
4#include <string>
5#include <string_view>
6#include <vector>
7
8// Maps ESBMC property-violation comment strings to MITRE CWE identifiers.
9//
10// Authoritative source: CWE 4.20 (published 2024-11-19).
11// Only entries whose Vulnerability Mapping Usage is ALLOWED or
12// ALLOWED-WITH-REVIEW are emitted; Discouraged / Prohibited ids are dropped.
13// See docs/cwe-mapping.md for the full table and rationale.
14
15// Single source of truth for one violation kind: the stable identifier used
16// by SARIF output, the human-readable short description used by SARIF / docs,
17// and the list of associated CWE ids.
19{
20 // SARIF `reportingDescriptor.id` value. SARIF §3.49.6 requires `id` to be a
21 // stable identifier; we use lowercase ASCII with hyphens so it is also a
22 // valid SARIF `simpleName` (§3.5.4), even though `name` is not emitted.
23 const char *sarif_id;
24 // Human-readable text used for `shortDescription` / docs / logs. May
25 // contain spaces, parentheses, slashes — anything non-`simpleName`.
26 const char *short_description;
27 // CWE ids associated with this violation kind (CWE 4.20).
28 std::vector<unsigned> cwes;
29};
30
31// Returns the rule for `comment`. `comment` is the freeform assertion text
32// (e.g. "dereference failure: NULL pointer"). Matching is first-match-wins
33// over an internal table whose keys are sorted longest-substring-first at
34// load time, so longer keys always win regardless of declaration order in
35// the source. Returns a fallback rule with empty `cwes` and
36// `sarif_id = "esbmc-assertion"` when no entry matches.
37//
38// Takes std::string_view so that callers passing `const char *` or
39// `std::string` do not construct a temporary `std::string` (and GCC's
40// -Wdangling-reference cannot mistake the returned reference for one bound
41// into a temporary).
42const cwe_rule_t &cwe_rule_for(std::string_view comment);
43
44// Convenience wrapper: returns `cwe_rule_for(comment).cwes`.
45std::vector<unsigned> cwe_for(std::string_view comment);
46
47// Returns the dedicated rule for the --dead-code-check advisory (CWE-561).
48// This is deliberately NOT part of the cwe_rule_for() substring table: dead
49// code is only ever emitted by the dead-code reporter, and folding it into the
50// generic matcher would mislabel any ordinary violation whose comment contains
51// the substring "dead code". See issue #4495.
53
54// Formats a list of ids as "CWE-476, CWE-125". Returns the empty string when
55// `ids` is empty.
56std::string format_cwe_list(const std::vector<unsigned> &ids);
57
58// Returns the short MITRE name for `id` (e.g. 476 -> "NULL Pointer
59// Dereference"). Returns an empty view for unknown ids.
60std::string_view cwe_name(unsigned id);
61
62#endif
const cwe_rule_t & dead_code_cwe_rule()
Definition cwe_mapping.cpp:222
const cwe_rule_t & cwe_rule_for(std::string_view comment)
Definition cwe_mapping.cpp:207
std::string format_cwe_list(const std::vector< unsigned > &ids)
Definition cwe_mapping.cpp:233
std::string_view cwe_name(unsigned id)
Definition cwe_mapping.cpp:246
std::vector< unsigned > cwe_for(std::string_view comment)
Definition cwe_mapping.cpp:217
Definition cwe_mapping.h:19
const char * sarif_id
Definition cwe_mapping.h:23
const char * short_description
Definition cwe_mapping.h:26
std::vector< unsigned > cwes
Definition cwe_mapping.h:28