fix(analyzer): per-package rule instantiation eliminates concurrent map crash#1589
Merged
ccojocar merged 1 commit intosecurego:masterfrom Mar 9, 2026
Conversation
…ap crash Analyzer.Process fans out package walks across goroutines that previously shared a single rule set. Rules with mutable per-package state — specifically readfile (G304), whose cleanedVar/joinedVar maps are written and read across goroutines — raced fatally under concurrency. The fix stores the RuleBuilder functions after LoadRules and calls buildPackageRuleset() at the start of each checkRules invocation, giving every concurrent worker its own freshly allocated rule instances with no shared mutable state and no locks required. Stale map entries no longer leak across package boundaries, closing a secondary false-negative bug as a side-effect. fixes: 1586
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1589 +/- ##
==========================================
+ Coverage 80.72% 80.83% +0.10%
==========================================
Files 104 104
Lines 9882 9901 +19
==========================================
+ Hits 7977 8003 +26
+ Misses 1418 1412 -6
+ Partials 487 486 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
ccojocar
approved these changes
Mar 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Analyzer.Processfans out togosec.concurrencygoroutines. Each goroutine callscheckRules(pkg)→ast.Walk→rule.Matchon the same shared rule instances registered once inLoadRules. Rules that maintain per-package state in struct fields — specificallyreadfile(G304) with itscleanedVarandjoinedVarmaps — are written and read concurrently across goroutines, producing a fatal map race:There is also a pre-existing secondary bug: the maps are never cleared between packages, so a variable resolved as "cleaned" in package
fooremains incleanedVarwhen packagebaris later analysed, silently suppressing findings.Approach
This fix is a direct elaboration of Option C from issue #1586 — creating a fresh rule instance per invocation of
checkRules- with two refinements:RuleBuilderfunctions.LoadRulesalready receivesmap[string]RuleBuilder. Storing these and re-invoking them incheckRulesproduces a complete, freshRuleSetwithout duplicating any constructor logic and without modifying any rule file.Changes (
analyzer.goonly — no rule files modified)AnalyzerstructruleBuilders map[string]RuleBuilderandruleSuppressed map[string]boolLoadRules()buildPackageRuleset()RuleSetcheckRules()buildPackageRuleset(), passes result intoastVisitorvia newruleset *RuleSetfieldastVisitorstructruleset *RuleSetfieldactiveRuleset()astVisitor— returns the package-local ruleset, falls back to sharedgosec.rulesetfor directCheckRulescallers (backward-compatible)Visit()activeRuleset()instead ofgosec.rulesetdirectlyReset()ruleBuildersandruleSuppressedProperties
CheckRules,LoadRules,Process,Report,Resetall have identical signatures.CheckRulesmethod (without going throughProcess) continue to use the sharedgosec.rulesetvia theactiveRuleset()fallback.sync.Map, no coordination needed now or in the future.