response body must be closed (bodyclose) when net/http is imported #121
-
Hello! 👋🏻 Not sure "bug" is the most appropriate category but it didn't fit in any other one (I didn't find "question") so here it is. The following piece of code triggers 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 My questions would be:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
This is almost certainly a false positive. All request-responses go through the func do(cl *http.Client, req *http.Request, validators []ResponseHandler, h ResponseHandler) (doResponse, error) {
res, err := cl.Do(req)
if err != nil {
return doConnect, err
}
defer res.Body.Close()
for _, v := range validators {
if v == nil {
continue
}
if err = v(res); err != nil {
return doValidate, err
}
}
if err = h(res); err != nil {
return doHandle, err
}
return doOK, nil
} https://github.com/earthboundkid/requests/blob/main/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. |
Beta Was this translation helpful? Give feedback.
-
I do get many bodyclose findings in the package itself. All of these are false positives?
|
Beta Was this translation helpful? Give feedback.
This is almost certainly a false positive. All request-responses go through the
do
function, which ensures Body.Close is called.