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

httptransport: check for err before deferring resp.Body.Close() #1173

Merged
merged 1 commit into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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