Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Case when calling Close() from struct field or method #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions passes/bodyclose/bodyclose.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,22 @@ func (r *runner) isopen(b *ssa.BasicBlock, i int) bool {
return r.calledInFunc(f, called)
}

// Case when calling Close() from struct field or method
if s, ok := aref.(*ssa.Store); ok {
if f, ok := s.Addr.(*ssa.FieldAddr); ok {
for _, bRef := range f.Block().Instrs {
bOp, ok := r.getBodyOp(bRef)
if !ok {
continue
}
for _, ccall := range *bOp.Referrers() {
if r.isCloseCall(ccall) {
return false
}
}
}
}
}
}
case *ssa.Call, *ssa.Defer: // Indirect function call
// Hacky way to extract CommonCall
Expand Down
27 changes: 27 additions & 0 deletions passes/bodyclose/testdata/src/a/issue42.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package a

import (
"net/http"
)

type MyResponse struct {
Original *http.Response
}

func (r *MyResponse) Response() *http.Response {
return r.Original
}

// issue42_1 is case when http.Response from struct field
func issue42_1() {
r := &MyResponse{}
r.Original, _ = http.Get("http://example.com/") // OK
_ = r.Original.Body.Close()
}

// issue42_2 is case when http.Response from a function
func issue42_2() {
r := &MyResponse{}
r.Original, _ = http.Get("http://example.com/") // OK
_ = r.Response().Body.Close()
}