golangci: enable bodyclose checker and fix related code

It has found an issue in the oracle code, so I think it's worth doing.
This commit is contained in:
Roman Khimov 2022-09-02 12:21:24 +03:00
parent df9a4fa7ce
commit 3c009271f8
7 changed files with 16 additions and 7 deletions

View file

@ -42,6 +42,7 @@ linters:
# extra linters
# - exhaustive
- bodyclose
- gofmt
- whitespace
- goimports

View file

@ -90,7 +90,10 @@ var errConnClosedByUser = errors.New("connection closed by user")
// operating on.
func NewWS(ctx context.Context, endpoint string, opts Options) (*WSClient, error) {
dialer := websocket.Dialer{HandshakeTimeout: opts.DialTimeout}
ws, _, err := dialer.Dial(endpoint, nil)
ws, resp, err := dialer.Dial(endpoint, nil)
if resp != nil && resp.Body != nil { // Can be non-nil even with error returned.
defer resp.Body.Close() // Not exactly required by websocket, but let's do this for bodyclose checker.
}
if err != nil {
return nil, err
}

View file

@ -40,7 +40,7 @@ func TestDefaultClient_RestrictedRedirectErr(t *testing.T) {
}
for _, c := range testCases {
t.Run(c, func(t *testing.T) {
_, err := cl.Get(c)
_, err := cl.Get(c) //nolint:bodyclose // It errors out and it's a test.
require.Error(t, err)
require.True(t, errors.Is(err, ErrRestrictedRedirect), err)
require.True(t, strings.Contains(err.Error(), "IP is not global unicast"), err)

View file

@ -138,6 +138,7 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error {
o.Log.Warn("oracle request failed", zap.String("url", req.Req.URL), zap.Error(err), zap.Stringer("code", resp.Code))
break
}
defer r.Body.Close()
switch r.StatusCode {
case http.StatusOK:
if !checkMediaType(r.Header.Get("Content-Type"), o.MainCfg.AllowedContentTypes) {

View file

@ -67,8 +67,6 @@ func (o *Oracle) AddResponse(pub *keys.PublicKey, reqID uint64, txSig []byte) {
var ErrResponseTooLarge = errors.New("too big response")
func readResponse(rc gio.ReadCloser, limit int) ([]byte, error) {
defer rc.Close()
buf := make([]byte, limit+1)
n, err := gio.ReadFull(rc, buf)
if err == gio.ErrUnexpectedEOF && n <= limit {

View file

@ -2633,8 +2633,9 @@ func checkErrGetBatchResult(t *testing.T, body []byte, expectingFail bool) json.
func doRPCCallOverWS(rpcCall string, url string, t *testing.T) []byte {
dialer := websocket.Dialer{HandshakeTimeout: time.Second}
url = "ws" + strings.TrimPrefix(url, "http")
c, _, err := dialer.Dial(url+"/ws", nil)
c, r, err := dialer.Dial(url+"/ws", nil)
require.NoError(t, err)
defer r.Body.Close()
err = c.SetWriteDeadline(time.Now().Add(time.Second))
require.NoError(t, err)
require.NoError(t, c.WriteMessage(1, []byte(rpcCall)))
@ -2651,6 +2652,7 @@ func doRPCCallOverHTTP(rpcCall string, url string, t *testing.T) []byte {
resp, err := cl.Post(url, "application/json", strings.NewReader(rpcCall))
require.NoErrorf(t, err, "could not make a POST request")
body, err := gio.ReadAll(resp.Body)
resp.Body.Close()
assert.NoErrorf(t, err, "could not read response from the request: %s", rpcCall)
return bytes.TrimSpace(body)
}

View file

@ -59,8 +59,9 @@ func initCleanServerAndWSClient(t *testing.T) (*core.Blockchain, *Server, *webso
dialer := websocket.Dialer{HandshakeTimeout: time.Second}
url := "ws" + strings.TrimPrefix(httpSrv.URL, "http") + "/ws"
ws, _, err := dialer.Dial(url, nil)
ws, r, err := dialer.Dial(url, nil)
require.NoError(t, err)
defer r.Body.Close()
// Use buffered channel to read server's messages and then read expected
// responses from it.
@ -520,7 +521,10 @@ func TestWSClientsLimit(t *testing.T) {
wss := make([]*websocket.Conn, maxSubscribers)
for i := 0; i < len(wss)+1; i++ {
ws, _, err := dialer.Dial(url, nil)
ws, r, err := dialer.Dial(url, nil)
if r != nil && r.Body != nil {
defer r.Body.Close()
}
if i < maxSubscribers {
require.NoError(t, err)
wss[i] = ws