detect Close calls in helper functions that receive resp.Body#80
Open
hugowetterberg wants to merge 1 commit intotimakin:masterfrom
Open
detect Close calls in helper functions that receive resp.Body#80hugowetterberg wants to merge 1 commit intotimakin:masterfrom
hugowetterberg wants to merge 1 commit intotimakin:masterfrom
Conversation
When resp.Body is passed as an argument to a helper function that calls Close on that parameter, bodyclose currently reports a false positive (see timakin#30). This happens because isBodyProperlyHandled only checks direct referrers of the body value for Close calls, but does not follow the body into function arguments. This change adds two methods: - closedViaHelper: checks if the body (or an interface-converted form of it) is passed to a function that closes it. - argClosedInCallee: resolves the static callee, maps the argument to the corresponding parameter, and checks if Close is called on that parameter's referrers. This covers common patterns like: defer safeClose("label", resp.Body) where safeClose calls body.Close() internally, as well as patterns where the body is first converted to io.Closer or io.ReadCloser before being passed to the helper. Test cases added for: - helper taking io.ReadCloser that closes it (should pass) - helper taking io.Closer that closes it (should pass) - helper taking io.ReadCloser that does NOT close it (should fail)
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.
When resp.Body is passed as an argument to a helper function that calls Close on that parameter, bodyclose currently reports a false positive (see #30).
This happens because isBodyProperlyHandled only checks direct referrers of the body value for Close calls, but does not follow the body into function arguments.
This change adds two methods:
This covers common patterns like:
where safeClose calls body.Close() internally, as well as patterns where the body is first converted to io.Closer or io.ReadCloser before being passed to the helper.
Test cases added for: