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 with carlmjohnson/requests package #71

Open
Crocmagnon opened this issue Dec 1, 2024 · 0 comments
Open

False positive with carlmjohnson/requests package #71

Crocmagnon opened this issue Dec 1, 2024 · 0 comments

Comments

@Crocmagnon
Copy link

Hello! 👋🏻

The following piece of code triggers bodyclose:

package main

import (
	"bytes"
	"context"
	"net/http"

	"github.com/carlmjohnson/requests"
)

func main() {
	var errResp bytes.Buffer

	_ = requests.URL("https://example.com/").
		Method(http.MethodPost).
		AddValidator(requests.ValidatorHandler(requests.DefaultValidator, requests.ToBytesBuffer(&errResp))).
		Fetch(context.Background())
}
$ golangci-lint run --enable bodyclose ./...
main.go:16:91: response body must be closed (bodyclose)
                AddValidator(requests.ValidatorHandler(requests.DefaultValidator, requests.ToBytesBuffer(&errResp))).

Interestingly, when removing the net/http import (for example by removing the Method or replacing it with an equivalent string), the issue disappears.

I first reported this to carlmjohnson/requests in this discussion and the package author seemed confident that the response body was indeed closed:

This is almost certainly a false positive. All request-responses go through the do function, which ensures Body.Close is called: https://github.com/earthboundkid/requests/blob/ed53f999490f0b01ce87a75cfeef18a53b970eef/core_do.go#L16-L36
I think the bug should be filed with the linter. I don't know what they are looking for, but it seems to be keying off the wrong thing in the requests package.

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

1 participant