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

net/http: document response body close error handling #58406

Open
AlexanderYastrebov opened this issue Feb 8, 2023 · 1 comment
Open

net/http: document response body close error handling #58406

AlexanderYastrebov opened this issue Feb 8, 2023 · 1 comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@AlexanderYastrebov
Copy link
Contributor

AlexanderYastrebov commented Feb 8, 2023

Documentation of the net/http package suggests in two examples https://pkg.go.dev/net/http#pkg-overview and https://pkg.go.dev/net/http#example-Get to use defer to close response body and thus ignore returned error:

resp, err := http.Get("http://example.com/")
if err != nil {
	// handle error
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
// ...

securego linter was recently changed to catch all unhandled errors of io.ReadCloser.Close and therefore it started to catch the very common defer resp.Body.Close() pattern, see discussion securego/gosec#925.

It would be great to update package documentation examples with a proper error handling of resp.Body.Close() (or confirm that defer resp.Body.Close() is a safe and recommended pattern).

@dr2chase
Copy link
Contributor

@neild can you give this a look?

@dr2chase dr2chase added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants