Skip to content

Commit

Permalink
httptransport: check for err before deferring resp.Body.Close()
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Zmeskal <jzmeskal@redhat.com>
  • Loading branch information
Jan Zmeskal authored and ldelossa committed Feb 5, 2021
1 parent f2d7313 commit df5e7f9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
17 changes: 4 additions & 13 deletions httptransport/client/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ func (s *HTTP) AffectedManifests(ctx context.Context, v []claircore.Vulnerabilit
return nil, fmt.Errorf("failed to create request: %v", err)
}
resp, err := s.c.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, &clairerror.ErrRequestFail{Code: resp.StatusCode, Status: resp.Status}
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&affected)
if err != nil {
return nil, &clairerror.ErrBadAffectedManifests{err}
Expand Down Expand Up @@ -73,12 +71,10 @@ func (s *HTTP) Index(ctx context.Context, manifest *claircore.Manifest) (*clairc
return nil, fmt.Errorf("failed to create request: %v", err)
}
resp, err := s.c.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, fmt.Errorf("failed to do request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, &clairerror.ErrRequestFail{Code: resp.StatusCode, Status: resp.Status}
}
Expand All @@ -103,12 +99,10 @@ func (s *HTTP) IndexReport(ctx context.Context, manifest claircore.Digest) (*cla
return nil, false, fmt.Errorf("failed to create request: %v", err)
}
resp, err := s.c.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, false, fmt.Errorf("failed to do request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return nil, false, nil
}
Expand All @@ -135,13 +129,10 @@ func (s *HTTP) State(ctx context.Context) (string, error) {
return "", fmt.Errorf("failed to create request: %v", err)
}
resp, err := s.c.Do(req)
if resp != nil {
defer resp.Body.Close()
}

if err != nil {
return "", fmt.Errorf("failed to do request: %v", err)
}
defer resp.Body.Close()
buf := &bytes.Buffer{}
if _, err := buf.ReadFrom(resp.Body); err != nil {
return "", err
Expand Down
16 changes: 4 additions & 12 deletions httptransport/client/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ func (c *HTTP) Scan(ctx context.Context, ir *claircore.IndexReport) (*claircore.
}

resp, err := c.c.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%v: unexpected status: %s", u.Path, resp.Status)
}
Expand Down Expand Up @@ -89,13 +87,11 @@ func (c *HTTP) DeleteUpdateOperations(ctx context.Context, ref ...uuid.UUID) (in
return
}
res, err := c.c.Do(req)
if res != nil {
defer res.Body.Close()
}
if err != nil {
errs[i] = err
return
}
defer res.Body.Close()
if got, want := res.StatusCode, http.StatusOK; got != want {
errs[i] = fmt.Errorf("%v: unexpected status: %s", u.Path, res.Status)
}
Expand Down Expand Up @@ -176,12 +172,10 @@ func (c *HTTP) LatestUpdateOperations(ctx context.Context) (map[string][]driver.
// if a subsequent response provides a StatusNotModified status, the map of UpdateOprations is served from cache.
func (c *HTTP) updateOperations(ctx context.Context, req *http.Request, cache *uoCache) (map[string][]driver.UpdateOperation, error) {
res, err := c.c.Do(req)
if res != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
defer res.Body.Close()
switch res.StatusCode {
case http.StatusOK:
m := make(map[string][]driver.UpdateOperation)
Expand Down Expand Up @@ -223,12 +217,10 @@ func (c *HTTP) UpdateDiff(ctx context.Context, prev, cur uuid.UUID) (*driver.Upd
req.URL.RawQuery = v.Encode()

res, err := c.c.Do(req)
if res != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%v: unexpected status: %s", u.Path, res.Status)
}
Expand Down

0 comments on commit df5e7f9

Please sign in to comment.