Skip to content

Commit

Permalink
Showing extended preview
Browse files Browse the repository at this point in the history
  • Loading branch information
kam193 committed Feb 17, 2025
1 parent 9e3a126 commit 32b1112
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion semgrep/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18
19
15 changes: 13 additions & 2 deletions semgrep/service/al_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,14 @@ def _process_results(
for result in results:
line_start, line_end = result["start"]["line"], result["end"]["line"]
if (line_start, line_end) not in lines_by_rule[result["check_id"]]:
line_no.add((line_start, line_end))
result_by_rule[result["check_id"]].append(result)
lines_by_rule[result["check_id"]].add((line_start, line_end))
extended_preview = self.metadata_cache.get(result["check_id"], {}).get(
"extend_preview", 0
)
if extended_preview:
line_start = max(line_start - extended_preview, first_line_no)
line_no.add((line_start, line_end))

lines = dict()
if line_no:
Expand All @@ -207,6 +212,9 @@ def _process_results(
metadata = self.metadata_cache.get(rule_id, {})
title = metadata.get("title", metadata.get("name", message[:100]))
attack_id = metadata.get("attack_id")
extend_preview = metadata.get("extend_preview", 0)
if not isinstance(extend_preview, int):
extend_preview = 0

section = ResultTextSection(
title,
Expand All @@ -216,7 +224,8 @@ def _process_results(
section.set_heuristic(heuristic, signature=rule_id, attack_id=attack_id)
for match in matches:
line_start, line_end = match["start"]["line"], match["end"]["line"]
line = lines.get((line_start, line_end), "")

line = lines.get((max(line_start - extend_preview, first_line_no), line_end), "")
code_hash = self._get_code_hash(line)

if first_line_no == 0:
Expand All @@ -225,6 +234,8 @@ def _process_results(
title = f"Match at lines {line_start} - {line_end}"
if line_start == line_end:
title = f"Match at line {line_start}"
if extend_preview:
title = f"{title} (+{extend_preview} lines before)"
ResultMemoryDumpSection(
title,
body=line[:MAX_LINE_SIZE],
Expand Down

0 comments on commit 32b1112

Please sign in to comment.