Skip to content

Commit 9a20107

Browse files
committed
fix(spotbugs): improve file path patching speed and accuracy
1 parent 022e977 commit 9a20107

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

codesectools/sasts/tools/SpotBugs/parser.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from __future__ import annotations
99

10+
from collections import defaultdict
1011
from pathlib import Path
1112
from typing import TYPE_CHECKING, Any
1213

@@ -29,30 +30,27 @@ class SpotBugsAnalysisResult(SARIFAnalysisResult):
2930
# /home/user/mypackage/src/main/java/org/mypackage/...
3031
def patch_dict(self, sarif_dict: dict) -> dict:
3132
"""Patch the SARIF dictionary to resolve relative Java class paths."""
32-
partial_parents = {}
33+
file_index = defaultdict(list)
34+
for file_path in self.source_path.rglob("*.java"): # SpotBugs only support Java
35+
file_index[file_path.name].append(file_path)
3336

3437
def recursive_patch(data: Any) -> None: # noqa: ANN401
3538
if isinstance(data, dict):
3639
for key, value in data.items():
3740
if key == "uri":
3841
partial_filepath = Path(value)
39-
if partial_filepath.parent not in partial_parents:
40-
if next(
41-
self.source_path.rglob(str(partial_filepath)), None
42+
candidates = file_index.get(partial_filepath.name, [])
43+
44+
found = None
45+
for candidate in candidates:
46+
if (
47+
candidate.parts[-len(partial_filepath.parts) :]
48+
== partial_filepath.parts
4249
):
43-
filepath = next(
44-
self.source_path.rglob(str(partial_filepath))
45-
).relative_to(self.source_path)
46-
partial_parents[partial_filepath.parent] = (
47-
filepath.parent
48-
)
49-
else:
50-
filepath = (
51-
partial_parents[partial_filepath.parent]
52-
/ partial_filepath.name
53-
)
50+
found = str(candidate.resolve())
51+
break
5452

55-
data[key] = str(filepath)
53+
data[key] = found if found else None
5654
else:
5755
recursive_patch(value)
5856

0 commit comments

Comments
 (0)