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

Returning *http.Response to another function is not handled correctly #11

Open
mkfsn opened this issue Apr 16, 2019 · 3 comments
Open

Comments

@mkfsn
Copy link

mkfsn commented Apr 16, 2019

Here is the sample code to reproduce:

package main

import (
	"net/http"
)

func getResponse(url string) *http.Response {
	res, _ := http.Get(url)
	return res
}

func main() {
	resp := getResponse("https://example.com")
	resp.Body.Close()
}

And the result of go vet:

go vet -vettool=$(which bodyclose) ./main.go
# command-line-arguments
./closer.go:8:20: response body must be closed
./closer.go:13:21: response body must be closed
@EmilGedda
Copy link

This also occurs when using a RequestRecorder and calling .Result() on them.

@natefinch
Copy link

natefinch commented Mar 3, 2022

Also occurs if you ever have a function that harmlessly passes through an HTTP response,
like this:

func addHeader(resp *http.Response) *http.Response {
    resp.Header.Set("foo", "bar")
    return resp
}

(clearly you don't even need to return the pointer, really, but that's beside the point, this is an actual example I've seen in the wild)

@kotaroyamazaki
Copy link

kotaroyamazaki commented May 30, 2022

This also occurs when returning a function that takes response as an argument, as in the following.

func modifyResponse() func(*http.Response) error {
	return func(resp *http.Response) error {
		return nil
	}
}
response body must be closed (bodyclose)
proxy.go:38:40:   proxy.ModifyResponse = modifyResponse()

SergeShpak added a commit to SergeShpak/bodyclose that referenced this issue Apr 19, 2023
SergeShpak added a commit to SergeShpak/bodyclose that referenced this issue Apr 19, 2023
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

4 participants