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

False Positive when http.Response from a function #42

Open
xxf0512 opened this issue Dec 19, 2022 · 2 comments
Open

False Positive when http.Response from a function #42

xxf0512 opened this issue Dec 19, 2022 · 2 comments

Comments

@xxf0512
Copy link

xxf0512 commented Dec 19, 2022

The following code triggers a false positive.

package main

import (
	"net/http"
	"tmp"
)

type MyResp struct {
	resp *http.Response
}

func (r *MyResp) Response() *http.Response {
	return r.resp
}

func test() {
	tmp := &MyResp{}
	var err error
	tmp.resp, err = http.Get("http://example.com/")
	if err != nil {
		fmt.Println(err)
	}
	defer tmp.Response().Body.Close()
	body, _ := ioutil.ReadAll(tmp.Response().Body)
	fmt.Println(body)
}

func main() {
        test()
}
@adamroyjones
Copy link

adamroyjones commented Jan 11, 2023

I came here to report a similar example. With

package main

import (
	"fmt"
	"io"
	"net/http"
)

func id[T any](r T) T { return r }

func main() {
	resp, err := http.Get("http://localhost:8008")
	if err != nil {
		panic(err)
	}

	resp = id(resp)
	defer resp.Body.Close()

	bs, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%s", bs)
}

I find that

go vet -vettool=$(which bodyclose) ./...

prints out

# falsepositive
./main.go:12:23: response body must be closed

(This is not sensitive to the use of generics.)

@Integralist
Copy link

I've just stumbled across this issue and it seems there's been a few other cases like this reported over the last year+ and no response from the author of the repo. Is it safe to presume this tool has been abandoned?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants