fix(G118): treat returned cancel func as called (fixes #1584)#1585
Merged
ccojocar merged 1 commit intosecurego:masterfrom Mar 6, 2026
Merged
Conversation
The isCancelCalled BFS did not handle *ssa.Return, so a cancel function returned from a helper was flagged as lost even though responsibility was transferred to the caller. Add a Return case that recognises the cancel value among the return operands and treats it as called. Update the existing 'cancel returned to caller' test to expect 0 issues, and add a new test case matching the exact pattern from issue securego#1584 (cancel returned as func() and invoked by the caller through a shutdown chain).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1585 +/- ##
=======================================
Coverage 80.71% 80.72%
=======================================
Files 104 104
Lines 9878 9882 +4
=======================================
+ Hits 7973 7977 +4
Misses 1418 1418
Partials 487 487 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Summary
Fixes #1584
The
isCancelCalledBFS in the G118 (context propagation) analyzer did not handle*ssa.Return, so a cancel function returned from a helper was flagged as "lost" even though responsibility was transferred to the caller.Root Cause
When a function like
initDatabasecallscontext.WithCancel(ctx)and returns the cancel func to its caller, the SSA representation produces aReturninstruction referencing the cancel value. The BFS inisCancelCalledhad nocase *ssa.Return:branch, so it dead-ended and concluded the cancel was never called.Changes
analyzers/context_propagation.go: Add a*ssa.Returncase to theisCancelCalledBFS — when the cancel value appears among the return operands, treat it as called (responsibility transferred to caller).testutils/g118_samples.go: Update the existing "cancel returned to caller" test to expect 0 issues. Add a new test case matching the exact issue bug: false positive on G118 #1584 pattern (cancel returned asfunc()and invoked by caller through a shutdown chain).Validation