Skip to content

Commit

Permalink
perf: Fix OOM issues caused by group-issues
Browse files Browse the repository at this point in the history
Group issues mechanism was extending the "from" and "name" arrays in
the current vuln it was iterating on, and only then assigning it to
acc[vuln.id]. This meant that for every vulns that needs to be grouped,
an array larger by 1 (of "from" and of "name") is added to the vuln.
In large vulns results this may cause may OOM issues,
because of data duplication.
This fix changes the the mechanism so that when acc[vuln.id] exists,
it adds the current vuln's 'from' and 'name' values to the existing
acc[vuln.id].from and name arrays.
  • Loading branch information
snaftaly committed Feb 9, 2022
1 parent 8751d7d commit 09215cf
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/lib/formatters/test/format-test-results.ts
Expand Up @@ -45,17 +45,26 @@ function createJsonResultOutput(jsonResult, options: Options) {

function formatJsonVulnerabilityStructure(jsonResult, options: Options) {
if (options['group-issues']) {
// Note: we have to reverse the array to keep the existing behavior so that the json output will stay the same.
// Since the entire array is reversed before grouping, we reverse it back after grouping to preserve the grouped vulns order.
const reversedVulnerabilities = jsonResult.vulnerabilities
? jsonResult.vulnerabilities.slice().reverse()
: [];
jsonResult.vulnerabilities = Object.values(
(jsonResult.vulnerabilities || []).reduce((acc, vuln): Record<
string,
any
> => {
vuln.from = [vuln.from].concat(acc[vuln.id]?.from || []);
vuln.name = [vuln.name].concat(acc[vuln.id]?.name || []);
acc[vuln.id] = vuln;
reversedVulnerabilities.reduce((acc, vuln): Record<string, any> => {
if (!acc[vuln.id]) {
acc[vuln.id] = {
...vuln,
from: [vuln.from],
name: [vuln.name],
};
} else {
acc[vuln.id].from.push(vuln.from);
acc[vuln.id].name.push(vuln.name);
}
return acc;
}, {}),
);
).reverse();
}

if (jsonResult.vulnerabilities) {
Expand Down

0 comments on commit 09215cf

Please sign in to comment.