Fix G118 false positive when cancel is stored in returned struct field#1593
Merged
ccojocar merged 1 commit intosecurego:masterfrom Mar 9, 2026
Merged
Conversation
When a cancel function is stored in a struct field and the struct is returned to the caller, the cancel responsibility is transferred. isCancelCalled did not detect this pattern because the FieldAddr trace reached a dead end — it never connected the field store to the struct being returned. Add isStructFieldReturnedFromFunc that checks whether the struct base pointer of a FieldAddr is loaded and returned, and call it from the Store+FieldAddr branch in isCancelCalled. Fixes securego#1591
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1593 +/- ##
=======================================
Coverage 80.87% 80.87%
=======================================
Files 104 104
Lines 9906 9920 +14
=======================================
+ Hits 8011 8023 +12
- Misses 1409 1410 +1
- Partials 486 487 +1 ☔ 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 #1591
The G118 analyzer (
detectLostCancel) reported a false positive when a cancel function was stored in a struct field and the struct was returned to the caller:Root cause
In
isCancelCalled, when the cancel value is stored into a struct field via aStoreto aFieldAddr, the trace adds theFieldAddraddress to the queue. But thatFieldAddr's only referrer is the Store itself — dead end. The struct is subsequently loaded and returned, transferring responsibility to the caller, but this link was never followed.The existing
isCancelCalledViaStructFieldonly checks methods on the same receiver type, which doesn't apply when the struct has no methods and the cancel is consumed by a plain caller.Fix
Added
isStructFieldReturnedFromFuncthat checks whether the struct base pointer from aFieldAddris loaded and returned from the enclosing function. Called from theStore+FieldAddrbranch inisCancelCalled— if the struct is returned, treat the cancel as transferred (same as a direct cancel return).Testing