Skip to content

Commit dd5babe

Browse files
committed
cwe-name-lookup: recognize "cwe_id", "names" as CSV header
... and insert the header into `cwe-names.csv` Closes: #118
1 parent b4a7f69 commit dd5babe

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

data/cwe-names.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"cwe_id","name"
12
"15","External Control of System or Configuration Setting"
23
"19","Data Processing Errors"
34
"20","Improper Input Validation"
@@ -306,7 +307,7 @@
306307
"663","Use of a Non-reentrant Function in a Concurrent Context"
307308
"664","Improper Control of a Resource Through its Lifetime"
308309
"665","Improper Initialization"
309-
"667", "Improper Locking"
310+
"667","Improper Locking"
310311
"670","Always-Incorrect Control Flow Implementation"
311312
"672","Operation on a Resource after Expiration or Release"
312313
"674","Uncontrolled Recursion"

src/lib/csv-parser.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ bool AbstractCsvParser::parse(InStream &ins)
5353
// break the current line into fields
5454
const TStringList fields(tok.begin(), tok.end());
5555

56-
// call the template method
56+
// call the template method for CSV header
57+
if (1 == d->lineno && /* handled */this->handleHeader(fields))
58+
continue;
59+
60+
// call the template method for CSV data
5761
if (!/* continue */this->handleLine(fields))
5862
break;
5963
}

src/lib/csv-parser.hh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ class AbstractCsvParser {
3939
protected:
4040
using TStringList = std::vector<std::string>;
4141

42+
/// called for the first line only
43+
virtual bool /* handled */ handleHeader(const TStringList &) {
44+
// returning false causes handleLine() to be called as a fallback
45+
return false;
46+
}
47+
4248
virtual bool /* continue */ handleLine(const TStringList &) = 0;
4349
void parseError(const std::string &msg);
4450

src/lib/cwe-name-lookup.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ CweNameLookup::CweNameLookup():
3636

3737
CweNameLookup::~CweNameLookup() = default;
3838

39+
bool CweNameLookup::handleHeader(const TStringList &fields)
40+
{
41+
// "cwe_id", "name" is recognized as the CSV header for cwe-names.csv
42+
return 2U == fields.size()
43+
&& "cwe_id" == fields[0]
44+
&& "name" == fields[1];
45+
}
46+
3947
bool CweNameLookup::handleLine(const TStringList &fields)
4048
{
4149
if (2U != fields.size()) {

src/lib/cwe-name-lookup.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class CweNameLookup: public AbstractCsvParser {
3434
const std::string& lookup(int cwe) const;
3535

3636
protected:
37+
bool /* handled */ handleHeader(const TStringList &) override;
3738
bool /* continue */ handleLine(const TStringList &) override;
3839

3940
private:

0 commit comments

Comments
 (0)