Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:416rehman DeepZero Silent Empty Results Guard

From Leeroopedia
Knowledge Sources
Domains Debugging, Static_Analysis
Last Updated 2026-10-27 00:00 GMT

Overview

Three-guard pattern in SemgrepScanner that distinguishes "scanned and found nothing" from "never scanned at all" — preventing silent false-negatives that would send every sample to LLM assessment described as clean.

Description

Semgrep can exit 0 with empty results for three very different reasons: the rules failed to load, the target files were never read, or the scan genuinely found nothing. Recording all three as "zero findings" would send samples on to LLM assessment described as clean, producing confident-but-wrong verdicts. The SemgrepScanner implements three explicit guards: (1) check semgrep's `errors` array when results are empty, (2) check whether semgrep's `paths.scanned` list is empty, and (3) fail loudly with a descriptive message rather than returning an empty-ok result. A parseable scan result is also treated as authoritative over the exit code, since semgrep emits complete output even with unexpected exit codes (observed on Windows).

Usage

Use this heuristic when designing any processor that wraps an external scanner or tool returning "empty results." Always distinguish between "tool ran and found nothing" and "tool did not run or read nothing." Never record a silent empty result as a clean verdict — fail the sample instead, so the pipeline operator can investigate.

The Insight (Rule of Thumb)

  • Action: After running semgrep, check three conditions before accepting empty results:
    1. Parse the output JSON even if the exit code is non-zero (exit code is not authoritative).
    2. If `errors` array is non-empty and `results` is empty, fail — rules did not load.
    3. If `paths.scanned` is an empty list, fail — no files were read; this is not a clean result.
  • Value: Fail with a descriptive message including the exit code and error detail (truncated to 500 chars).
  • Trade-off: Failing on ambiguous empty results may cause some samples to need re-runs, but prevents confident false-negatives from reaching LLM assessment.
  • Compatibility: Applies to `SemgrepScanner`; the pattern generalizes to any external scanner wrapper.

Reasoning

A scan that reads none of the submitted files is not the same result as reading them and finding nothing. The first means the scan never looked; recording it as zero findings would describe every sample as clean. Semgrep reports rule/config load failures in an `errors` array while still exiting 0 with empty results, and lists scanned files in `paths.scanned`. Both are checked explicitly. Additionally, semgrep emits a complete results document on stdout even with an unexpected exit code (observed on Windows), so a parseable result is authoritative over the exit code — only treat the run as failed when no usable output came back.

Code Evidence

Guard 1 — errors array with empty results from `semgrep_scanner.py:254-264`:

# semgrep reports rule/config load failures in an "errors" array while
# still exiting 0 with empty results - which would otherwise look like
# "no vulnerabilities found". fail loudly when nothing scanned.
scan_errors = output.get("errors") or []
if scan_errors and not output.get("results"):
    detail = "; ".join(
        str(e.get("message") or e.get("type") or e) for e in scan_errors[:3]
    )[:500]
    for idx, _ in uncached_entries:
        results[idx] = ProcessorResult.fail(f"semgrep produced no results: {detail}")
    return

Guard 2 — no files scanned from `semgrep_scanner.py:269-281`:

# semgrep lists the files it actually read. Reading none of them is not
# the same result as reading them and finding nothing: the first means
# the scan never looked, and recording it as zero findings would send
# every sample on to assessment described as clean.
scanned_paths = (output.get("paths") or {}).get("scanned")
if isinstance(scanned_paths, list) and not scanned_paths:
    detail = (err_str.strip() or "semgrep listed no scanned files")[:500]
    for idx, _ in uncached_entries:
        results[idx] = ProcessorResult.fail(
            f"semgrep read none of the {len(file_to_sample)} files submitted, "
            f"so this is not a clean result: {detail}"
        )
    return

Guard 3 — parseable output authoritative over exit code from `semgrep_scanner.py:232-252`:

# semgrep emits a complete results document on stdout even when it exits
# with an unexpected code (observed on windows), so a parseable scan
# result is authoritative over the exit code. only treat the run as
# failed when no usable output came back - and always report the exit
# code, never an empty "semgrep error:".
output: dict[str, Any] | None = None
if out_str.strip():
    try:
        parsed = json.loads(out_str)
        if isinstance(parsed, dict) and "results" in parsed:
            output = parsed
    except json.JSONDecodeError:
        output = None

if output is None:
    detail = (err_str.strip() or "no readable results document")[:500]
    for idx, _ in uncached_entries:
        results[idx] = ProcessorResult.fail(
            f"semgrep failed (exit {proc.returncode}): {detail}"
        )
    return

Rules-path resolution consistency from `semgrep_scanner.py:33-44`:

# resolve rules_dir consistently for validate() and process(): try
# cwd-relative first (how the shipped pipelines reference their rules),
# then relative to the pipeline directory. resolving these differently
# let validation pass while the scan silently ran with no rules loaded.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment