Skip to content

Commit efe8291

Browse files
authored
Call Close() on files and HTTP response bodies (#564)
Signed-off-by: Caleb Xu <caxu@redhat.com>
1 parent 2c6d905 commit efe8291

File tree

6 files changed

+11
-6
lines changed

6 files changed

+11
-6
lines changed

cmd/report.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func NewReportCmd(config *viper.Viper) *cobra.Command {
8080
if openErr != nil {
8181
return fmt.Errorf("report path %s: error opening file %v", reportArg, openErr)
8282
}
83+
defer reportFile.Close()
8384

8485
reportBytes, readErr := io.ReadAll(reportFile)
8586
if readErr != nil {

internal/chartverifier/checks/checks.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ func SignatureIsValid(opts *CheckOptions) (Result, error) {
361361
if err != nil {
362362
return NewResult(false, fmt.Sprintf("%s : get error was %v", SignatureFailure, err)), nil
363363
}
364+
defer resp.Body.Close()
364365

365366
if resp.StatusCode == 404 {
366367
return NewSkippedResult(fmt.Sprintf("%s : %s", ChartNotSigned, SignatureIsNotPresentSuccess)), nil
@@ -458,6 +459,8 @@ func downloadFile(fileURL *url.URL, directory string) (string, error) {
458459
if err != nil {
459460
return "", err
460461
}
462+
defer file.Close()
463+
461464
client := http.Client{
462465
CheckRedirect: func(r *http.Request, via []*http.Request) error {
463466
r.URL.Opaque = r.URL.Path
@@ -476,8 +479,6 @@ func downloadFile(fileURL *url.URL, directory string) (string, error) {
476479
if err != nil {
477480
return "", err
478481
}
479-
// #nosec G307
480-
defer file.Close()
481482

482483
return filePath, nil
483484
}

internal/chartverifier/checks/helm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func loadChartFromRemote(url *url.URL) (*chart.Chart, error) {
5353
if err != nil {
5454
return nil, err
5555
}
56+
defer resp.Body.Close()
5657

5758
if resp.StatusCode == http.StatusNotFound {
5859
return nil, ChartNotFoundErr(url.String())

internal/chartverifier/pyxis/pyxis.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ func GetImageRegistries(repository string) ([]string, error) {
9595
if reqErr != nil {
9696
err = fmt.Errorf("error getting repository %s : %v", repository, err)
9797
} else {
98+
// #nosec G307
99+
defer resp.Body.Close()
98100
if resp.StatusCode == 200 {
99-
// #nosec G307
100-
defer resp.Body.Close()
101101
body, _ := io.ReadAll(resp.Body)
102102
var repositoriesBody RepositoriesBody
103103
//nolint:errcheck // TODO(komish): this should be checked, but we really need
@@ -162,9 +162,9 @@ Loops:
162162
resp, reqErr := client.Do(req)
163163

164164
if reqErr == nil {
165+
// #nosec G307
166+
defer resp.Body.Close()
165167
if resp.StatusCode == 200 {
166-
// #nosec G307
167-
defer resp.Body.Close()
168168
// TODO(komish): this should be checked, but we really need
169169
// to look at refactoring this block in its entirety. Delay fixing this until then
170170
//

pkg/chartverifier/report/report.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func loadReportFromRemote(url *url.URL) (string, error) {
132132
if getErr != nil {
133133
return "", fmt.Errorf("report uri %s: error reading from url %v", url, getErr)
134134
}
135+
defer resp.Body.Close()
135136

136137
if resp.StatusCode == http.StatusNotFound {
137138
return "", fmt.Errorf("report uri %s: bad response reading from url %d", url, resp.StatusCode)

pkg/chartverifier/reportsummary/reportsummary_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ func loadChartFromAbsPath(path string) ([]byte, error) {
304304
if openErr != nil {
305305
return nil, fmt.Errorf("report path %s: error opening file %v", path, openErr)
306306
}
307+
defer reportFile.Close()
307308

308309
reportBytes, readErr := io.ReadAll(reportFile)
309310
if readErr != nil {

0 commit comments

Comments
 (0)