Skip to content

Commit 92b64a4

Browse files
committed
parser-json-sarif: introduce readToolInfo() helper
... in order to make `readScanProps()` easier to extend. No change in behavior intended with this commit.
1 parent 5ce64c7 commit 92b64a4

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

src/lib/parser-json-sarif.cc

+44-33
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
struct SarifTreeDecoder::Private {
2828
void updateCweMap(const pt::ptree *driverNode);
29+
void readToolInfo(TScanProps *pScanProps, const pt::ptree *toolNode);
2930

3031
std::string singleChecker = "UNKNOWN_SARIF_WARNING";
3132
const RE reCwe = RE("^CWE-([0-9]+)$");
@@ -80,35 +81,15 @@ void SarifTreeDecoder::Private::updateCweMap(const pt::ptree *driverNode)
8081
}
8182
}
8283

83-
void SarifTreeDecoder::readScanProps(
84-
TScanProps *pDst,
85-
const pt::ptree *root)
84+
void SarifTreeDecoder::Private::readToolInfo(
85+
TScanProps *pScanProps,
86+
const pt::ptree *toolNode)
8687
{
87-
// read external properties if available
88-
const pt::ptree *iep;
89-
if (findChildOf(&iep, *root, "inlineExternalProperties")
90-
&& (1U == iep->size()))
91-
{
92-
const pt::ptree *props;
93-
if (findChildOf(&props, iep->begin()->second, "externalizedProperties"))
94-
for (const pt::ptree::value_type &item : *props)
95-
(*pDst)[item.first] = item.second.data();
96-
}
97-
98-
// check that we have exactly one run
99-
const pt::ptree *runs;
100-
if (!findChildOf(&runs, *root, "runs") || (1U != runs->size()))
101-
return;
102-
103-
// check which tool was used for the run
104-
const pt::ptree *toolNode;
105-
if (!findChildOf(&toolNode, runs->begin()->second, "tool"))
106-
return;
10788
const pt::ptree *driverNode;
10889
if (!findChildOf(&driverNode, *toolNode, "driver"))
10990
return;
11091

111-
d->updateCweMap(driverNode);
92+
this->updateCweMap(driverNode);
11293

11394
const auto name = valueOf<std::string>(*driverNode, "name");
11495
auto version = valueOf<std::string>(*driverNode, "version");
@@ -117,36 +98,66 @@ void SarifTreeDecoder::readScanProps(
11798

11899
if (name == "SnykCode") {
119100
// Snyk Code detected!
120-
d->singleChecker = "SNYK_CODE_WARNING";
101+
this->singleChecker = "SNYK_CODE_WARNING";
121102

122103
if (!version.empty())
123104
// record tool version of Snyk Code
124-
(*pDst)["analyzer-version-snyk-code"] = std::move(version);
105+
(*pScanProps)["analyzer-version-snyk-code"] = std::move(version);
125106
}
126107
else if (name == "gitleaks") {
127108
// gitleaks
128-
d->singleChecker = "GITLEAKS_WARNING";
109+
this->singleChecker = "GITLEAKS_WARNING";
129110

130111
if (!version.empty())
131-
(*pDst)["analyzer-version-gitleaks"] = std::move(version);
112+
(*pScanProps)["analyzer-version-gitleaks"] = std::move(version);
132113
}
133114
else if (name == "Semgrep OSS") {
134115
// semgrep
135-
d->singleChecker = "SEMGREP_WARNING";
116+
this->singleChecker = "SEMGREP_WARNING";
136117

137118
if (!version.empty())
138-
(*pDst)["analyzer-version-semgrep"] = std::move(version);
119+
(*pScanProps)["analyzer-version-semgrep"] = std::move(version);
139120
}
140121
else if (boost::starts_with(name, "GNU C")) {
141122
// GCC
142-
d->singleChecker = "COMPILER_WARNING";
123+
this->singleChecker = "COMPILER_WARNING";
143124

144125
boost::smatch sm;
145-
if (boost::regex_match(version, sm, d->reVersion))
146-
(*pDst)["analyzer-version-gcc"] = sm[/* version */ 1];
126+
if (boost::regex_match(version, sm, this->reVersion))
127+
(*pScanProps)["analyzer-version-gcc"] = sm[/* version */ 1];
147128
}
148129
}
149130

131+
void SarifTreeDecoder::readScanProps(
132+
TScanProps *pDst,
133+
const pt::ptree *root)
134+
{
135+
// read external properties if available
136+
const pt::ptree *iep;
137+
if (findChildOf(&iep, *root, "inlineExternalProperties")
138+
&& (1U == iep->size()))
139+
{
140+
const pt::ptree *props;
141+
if (findChildOf(&props, iep->begin()->second, "externalizedProperties"))
142+
for (const pt::ptree::value_type &item : *props)
143+
(*pDst)[item.first] = item.second.data();
144+
}
145+
146+
// check that we have exactly one run
147+
const pt::ptree *runs;
148+
if (!findChildOf(&runs, *root, "runs")
149+
|| /* TODO: warn bout unsupported format */ (1U != runs->size()))
150+
return;
151+
152+
// jump to the only run
153+
const pt::ptree &run0 = runs->begin()->second;
154+
155+
// check which tool was used for the run
156+
const pt::ptree *toolNode;
157+
if (findChildOf(&toolNode, run0, "tool"))
158+
d->readToolInfo(pDst, toolNode);
159+
}
160+
150161
void SarifTreeDecoder::readRoot(const pt::ptree *runs)
151162
{
152163
if (1U != runs->size())

0 commit comments

Comments
 (0)