Skip to content

fix: skip SSA analysis on ill-typed packages to prevent panic - #1607

Merged
ccojocar merged 1 commit into
securego:masterfrom
ccojocar:fix/1604-ssa-panic-ill-typed-package
Mar 13, 2026
Merged

ccojocar merged 1 commit into
securego:masterfrom
ccojocar:fix/1604-ssa-panic-ill-typed-package

Conversation

@ccojocar

Copy link
Copy Markdown
Member

Summary

Packages with type errors (pkg.IllTyped == true) have partial type information that can cause nil pointer dereferences inside the SSA builder (golang.org/x/tools/go/ssa.emitConv). This results in a panic during gosec scans, even though the panic is caught by defer/recover — users still see a frightening 50+ line stack trace.

Root Cause

When packages.Load processes a package with compilation errors, it still populates pkg.Types and pkg.TypesInfo with partial data. Some TypesInfo.Types entries have nil Type fields for expressions the type checker couldn't resolve. The SSA builder doesn't guard against these nil types in emitConv, causing a nil pointer dereference.

The existing guards in buildSSA check for pkg.Types == nil and pkg.TypesInfo == nil, but miss the case where these fields are populated with incomplete data.

Fix

Add a pkg.IllTyped check in buildSSA (after the existing nil guards) that returns a clean error before attempting SSA construction. AST-based rules continue to run normally on the affected package.

Before:

[gosec] Panic when running SSA analyzer on package: models. Panic: runtime error: invalid memory address or nil pointer dereference
Stack trace:
goroutine 54 [running]:
runtime/debug.Stack()
... (50+ lines)

After:

[gosec] Error building the SSA representation of the package models: package models has type errors, skipping SSA analysis

Changes

  • analyzer.go: Add pkg.IllTyped guard in buildSSA
  • analyzer_core_internal_test.go: Add TestBuildSSAIllTypedPackage

Fixes #1604

Packages with type errors (pkg.IllTyped == true) have partial type
information that can cause nil pointer dereferences inside the SSA
builder (golang.org/x/tools/go/ssa.emitConv).

Add a pkg.IllTyped guard in buildSSA that returns a clean error
instead of letting the SSA builder panic. AST-based rules continue
to run normally on the affected package.

Fixes securego#1604
@codecov

codecov Bot commented Mar 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.02%. Comparing base (1ced32d) to head (2eaae81).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1607   +/-   ##
=======================================
  Coverage   81.01%   81.02%           
=======================================
  Files         108      108           
  Lines       10010    10012    +2     
=======================================
+ Hits         8110     8112    +2     
  Misses       1418     1418           
  Partials      482      482           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ccojocar
ccojocar merged commit 8aec3f4 into securego:master Mar 13, 2026
9 checks passed
@ccojocar
ccojocar deleted the fix/1604-ssa-panic-ill-typed-package branch March 13, 2026 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A security scan using Gosec 2.22.7 works fine, but results in a panic in latest version 2.24.7

1 participant