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 · May be fixed by #65
Open

False Positive when http.Response from a function #42

xxf0512 opened this issue Dec 19, 2022 · 2 comments · May be fixed by #65

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?

@ma91n ma91n linked a pull request Sep 9, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

3 participants