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

fix: fix memory leak on failed http requests #59

Merged
merged 1 commit into from
Feb 5, 2025
Merged
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: 13 additions & 4 deletions cmd/csaf_downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,16 @@ nextAdvisory:
"error", err)
continue
}
responseBody, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
stats.downloadFailed++
errorCh <- csafErrs.ErrNetwork{Message: fmt.Sprintf("can't read response body containing CSAF document %s from URL %s: %v", filename, file.URL(), err)}
slog.Warn("Cannot read response body",
"url", file.URL(),
"error", err)
continue
}

if resp.StatusCode != http.StatusOK {
switch {
Expand Down Expand Up @@ -548,8 +558,7 @@ nextAdvisory:
var doc any

if err := func() error {
defer resp.Body.Close()
tee := io.TeeReader(resp.Body, hasher)
tee := io.TeeReader(bytes.NewReader(responseBody), hasher)
return json.NewDecoder(tee).Decode(&doc)
}(); err != nil {
stats.downloadFailed++
Expand Down Expand Up @@ -766,11 +775,11 @@ func loadSignature(client util.Client, p string) (*crypto.PGPSignature, []byte,
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf(
"fetching signature from '%s' failed: %s (%d)", p, resp.Status, resp.StatusCode)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
Expand All @@ -787,11 +796,11 @@ func loadHash(client util.Client, p string) ([]byte, []byte, error) {
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf(
"fetching hash from '%s' failed: %s (%d)", p, resp.Status, resp.StatusCode)
}
defer resp.Body.Close()
var data bytes.Buffer
tee := io.TeeReader(resp.Body, &data)
hash, err := util.HashFromReader(tee)
Expand Down
Loading