Skip to content

Commit bdbb39f

Browse files
authored
Replace uses of *zip.ReadCloser with *zip.Reader when Close is not used (#992)
1 parent 07a65fa commit bdbb39f

File tree

13 files changed

+38
-32
lines changed

13 files changed

+38
-32
lines changed

cmd/updater/common/dump_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func OpenGenesisDumpAndExtractManifest(zipPath string) (*zip.ReadCloser, *vulndu
1919
if err != nil {
2020
return nil, nil, errors.Wrap(err, "opening zip")
2121
}
22-
manifest, err := vulndump.LoadManifestFromDump(zipR)
22+
manifest, err := vulndump.LoadManifestFromDump(&zipR.Reader)
2323
if err != nil {
2424
return nil, nil, err
2525
}

cmd/updater/diffdumps/cmd.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ func generateIstioDiff(outputDir string, baseF, headF *zip.File) error {
113113
return nil
114114
}
115115

116-
func generateK8sDiffs(outputDir string, baseZipR *zip.ReadCloser, headZipR *zip.ReadCloser) error {
116+
func generateK8sDiffs(outputDir string, baseZipR, headZipR *zip.Reader) error {
117117
return generateDiffsHelper(outputDir, baseZipR, headZipR, vulndump.K8sDirName, generateK8sDiff)
118118
}
119119

120-
func generateIstioDiffs(outputDir string, baseZipR *zip.ReadCloser, headZipR *zip.ReadCloser) error {
120+
func generateIstioDiffs(outputDir string, baseZipR, headZipR *zip.Reader) error {
121121
return generateDiffsHelper(outputDir, baseZipR, headZipR, vulndump.IstioDirName, generateIstioDiff)
122122
}
123123

124-
func generateDiffsHelper(outputDir string, baseZipR *zip.ReadCloser, headZipR *zip.ReadCloser, dirName string, generateDiffs generateDiffFunc) error {
124+
func generateDiffsHelper(outputDir string, baseZipR, headZipR *zip.Reader, dirName string, generateDiffs generateDiffFunc) error {
125125
subDir := filepath.Join(outputDir, dirName)
126126
if err := os.MkdirAll(subDir, 0755); err != nil {
127127
return errors.Wrap(err, "creating subdir for Kubernetes")
@@ -198,7 +198,7 @@ func generateNVDDiff(outputDir string, baseLastModifiedTime time.Time, headF *zi
198198
return nil
199199
}
200200

201-
func generateNVDDiffs(outputDir string, baseLastModifiedTime time.Time, headZipR *zip.ReadCloser) error {
201+
func generateNVDDiffs(outputDir string, baseLastModifiedTime time.Time, headZipR *zip.Reader) error {
202202
nvdSubDir := filepath.Join(outputDir, vulndump.NVDDirName)
203203
if err := os.MkdirAll(nvdSubDir, 0755); err != nil {
204204
return errors.Wrap(err, "creating subdir for NVD")
@@ -321,7 +321,7 @@ func updateAlpineLink(cfg config, vuln *database.Vulnerability) {
321321
}
322322
}
323323

324-
func generateOSVulnsDiff(outputDir string, baseZipR, headZipR *zip.ReadCloser, cfg config) error {
324+
func generateOSVulnsDiff(outputDir string, baseZipR, headZipR *zip.Reader, cfg config) error {
325325
baseVulns, err := vulndump.LoadOSVulnsFromDump(baseZipR)
326326
if err != nil {
327327
return errors.Wrap(err, "loading OS vulns from base dump")
@@ -436,6 +436,7 @@ func Command() *cobra.Command {
436436
if err != nil {
437437
return errors.Wrap(err, "loading head dump")
438438
}
439+
defer utils.IgnoreError(headZipR.Close)
439440

440441
// Intentionally return an error even in the equal case, they are only going to be equal if two dumps are literally
441442
// exactly the same, and that's probably a mistake by the invoker of the program.
@@ -456,7 +457,7 @@ func Command() *cobra.Command {
456457
log.Info("Skipping Kubernetes diff")
457458
} else {
458459
log.Info("Generating Kubernetes diff...")
459-
if err := generateK8sDiffs(stagingDir, baseZipR, headZipR); err != nil {
460+
if err := generateK8sDiffs(stagingDir, &baseZipR.Reader, &headZipR.Reader); err != nil {
460461
return errors.Wrap(err, "creating Kubernetes diff")
461462
}
462463
log.Info("Done generating Kubernetes diff")
@@ -466,20 +467,20 @@ func Command() *cobra.Command {
466467
log.Info("Skipping Istio diff")
467468
} else {
468469
log.Info("Generating Istio diff...")
469-
if err := generateIstioDiffs(stagingDir, baseZipR, headZipR); err != nil {
470+
if err := generateIstioDiffs(stagingDir, &baseZipR.Reader, &headZipR.Reader); err != nil {
470471
return errors.Wrap(err, "creating Istio diff")
471472
}
472473
log.Info("Done generating Isio diff")
473474
}
474475

475476
log.Info("Generating NVD diff...")
476-
if err := generateNVDDiffs(stagingDir, baseManifest.Until, headZipR); err != nil {
477+
if err := generateNVDDiffs(stagingDir, baseManifest.Until, &headZipR.Reader); err != nil {
477478
return errors.Wrap(err, "creating NVD diff")
478479
}
479480
log.Info("Done generating NVD diff")
480481

481482
log.Info("Generating OS vulns diff...")
482-
if err := generateOSVulnsDiff(stagingDir, baseZipR, headZipR, cfg); err != nil {
483+
if err := generateOSVulnsDiff(stagingDir, &baseZipR.Reader, &headZipR.Reader, cfg); err != nil {
483484
return errors.Wrap(err, "creating OS vulns diff")
484485
}
485486
log.Info("Generated OS vulns diff")
@@ -488,7 +489,7 @@ func Command() *cobra.Command {
488489
log.Info("Skipping RHELv2 diff")
489490
} else {
490491
log.Info("Generating RHELv2 vulns diff")
491-
if err := generateRHELv2VulnsDiff(cfg, stagingDir, baseManifest.Until, baseZipR, headZipR); err != nil {
492+
if err := generateRHELv2VulnsDiff(cfg, stagingDir, baseManifest.Until, &baseZipR.Reader, &headZipR.Reader); err != nil {
492493
return errors.Wrap(err, "creating RHELv2 vulns diff")
493494
}
494495
log.Info("Generated RHELv2 vulns diff")

cmd/updater/diffdumps/rhelv2_diff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func generateRHELv2Diff(cfg config, outputDir string, baseLastModifiedTime time.
153153
return nil
154154
}
155155

156-
func generateRHELv2VulnsDiff(cfg config, outputDir string, baseLastModifiedTime time.Time, baseZipR, headZipR *zip.ReadCloser) error {
156+
func generateRHELv2VulnsDiff(cfg config, outputDir string, baseLastModifiedTime time.Time, baseZipR, headZipR *zip.Reader) error {
157157
rhelv2VulnsSubDir := filepath.Join(outputDir, vulndump.RHELv2DirName, vulndump.RHELv2VulnsSubDirName)
158158
if err := os.MkdirAll(rhelv2VulnsSubDir, 0755); err != nil {
159159
return errors.Wrap(err, "creating subdir for RHELv2")

cmd/updater/printstats/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func Command() *cobra.Command {
2323
return errors.Wrap(err, "loading dump")
2424
}
2525
defer utils.IgnoreError(zipR.Close)
26-
vulns, err := vulndump.LoadOSVulnsFromDump(zipR)
26+
vulns, err := vulndump.LoadOSVulnsFromDump(&zipR.Reader)
2727
if err != nil {
2828
return errors.Wrap(err, "loading os vulns from dump")
2929
}

cpe/nvdtoolscache/db.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ func New() (Cache, error) {
5656
return newWithDB(db), nil
5757
}
5858

59+
var _ Cache = (*cacheImpl)(nil)
60+
5961
type cacheImpl struct {
6062
*bbolt.DB
6163

cpe/nvdtoolscache/load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (c *cacheImpl) LoadFromDirectory(definitionsDir string) error {
4040
return nil
4141
}
4242

43-
func (c *cacheImpl) LoadFromZip(zipR *zip.ReadCloser, definitionsDir string) error {
43+
func (c *cacheImpl) LoadFromZip(zipR *zip.Reader, definitionsDir string) error {
4444
log.WithField("dir", definitionsDir).Info("Loading definitions directory")
4545

4646
readers, err := ziputil.OpenFilesInDir(zipR, definitionsDir, ".json")

k8s/cache/db.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/stackrox/scanner/pkg/vulndump"
1010
)
1111

12+
var _ Cache = (*cacheImpl)(nil)
13+
1214
type cacheImpl struct {
1315
cacheRWLock sync.RWMutex
1416
// The expectation is that the number of Kubernetes vulns is rather low (100 or fewer).

k8s/cache/load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (c *cacheImpl) LoadFromDirectory(definitionsDir string) error {
5454
return nil
5555
}
5656

57-
func (c *cacheImpl) LoadFromZip(zipR *zip.ReadCloser, definitionsDir string) error {
57+
func (c *cacheImpl) LoadFromZip(zipR *zip.Reader, definitionsDir string) error {
5858
log.WithField("dir", definitionsDir).Info("Loading definitions directory")
5959

6060
rs, err := ziputil.OpenFilesInDir(zipR, definitionsDir, ".yaml")

pkg/cache/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
type Cache interface {
1010
Dir() string
1111
LoadFromDirectory(definitionsDir string) error
12-
LoadFromZip(zipR *zip.ReadCloser, definitionsDir string) error
12+
LoadFromZip(zipR *zip.Reader, definitionsDir string) error
1313
GetLastUpdate() time.Time
1414
SetLastUpdate(t time.Time)
1515
}

pkg/repo2cpe/mapping.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (m *Mapping) LoadFile(path string) error {
7171
}
7272

7373
// LoadFromZip reads the repo-to-cpe file in the given directory from the given zip reader.
74-
func (m *Mapping) LoadFromZip(zipR *zip.ReadCloser, dir string) error {
74+
func (m *Mapping) LoadFromZip(zipR *zip.Reader, dir string) error {
7575
path := filepath.Join(dir, RHELv2CPERepoName)
7676
r, err := ziputil.OpenFile(zipR, path)
7777
if err != nil {

pkg/vulndump/loader.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ var (
3030
updateLockName = "update"
3131
)
3232

33-
func validateAndLoadManifest(f io.ReadCloser) (*Manifest, error) {
34-
defer utils.IgnoreError(f.Close)
33+
func validateAndLoadManifest(f io.Reader) (*Manifest, error) {
3534
var m Manifest
3635
err := json.NewDecoder(f).Decode(&m)
3736
if err != nil {
@@ -89,11 +88,13 @@ func determineWhetherToUpdate(db database.Datastore, manifest *Manifest) (bool,
8988
}
9089

9190
// LoadManifestFromDump validates and loads the manifest from the given zip file.
92-
func LoadManifestFromDump(zipR *zip.ReadCloser) (*Manifest, error) {
91+
func LoadManifestFromDump(zipR *zip.Reader) (*Manifest, error) {
9392
manifestFile, err := ziputil.OpenFile(zipR, ManifestFileName)
9493
if err != nil {
9594
return nil, errors.Wrap(err, "opening manifest file")
9695
}
96+
defer utils.IgnoreError(manifestFile.Close)
97+
9798
manifest, err := validateAndLoadManifest(manifestFile)
9899
if err != nil {
99100
return nil, errors.Wrap(err, "loading/validating manifest")
@@ -102,7 +103,7 @@ func LoadManifestFromDump(zipR *zip.ReadCloser) (*Manifest, error) {
102103
}
103104

104105
// LoadOSVulnsFromDump loads the os vulns file from the dump into an in-memory slice.
105-
func LoadOSVulnsFromDump(zipR *zip.ReadCloser) ([]database.Vulnerability, error) {
106+
func LoadOSVulnsFromDump(zipR *zip.Reader) ([]database.Vulnerability, error) {
106107
osVulnsFile, err := ziputil.OpenFile(zipR, OSVulnsFileName)
107108
if err != nil {
108109
return nil, errors.Wrap(err, "opening OS vulns file")
@@ -185,7 +186,7 @@ func startVulnLoad(manifest *Manifest, db database.Datastore, updateInterval tim
185186
}, nil
186187
}
187188

188-
func loadOSVulns(zipR *zip.ReadCloser, db database.Datastore) error {
189+
func loadOSVulns(zipR *zip.Reader, db database.Datastore) error {
189190
log.Info("Loading OS vulns...")
190191
osVulns, err := LoadOSVulnsFromDump(zipR)
191192
if err != nil {
@@ -200,7 +201,7 @@ func loadOSVulns(zipR *zip.ReadCloser, db database.Datastore) error {
200201
return nil
201202
}
202203

203-
func loadRHELv2Vulns(db database.Datastore, zipR *zip.ReadCloser, repoToCPE *repo2cpe.Mapping) error {
204+
func loadRHELv2Vulns(db database.Datastore, zipR *zip.Reader, repoToCPE *repo2cpe.Mapping) error {
204205
log.Info("Loading RHELv2 vulns...")
205206
if repoToCPE != nil {
206207
if err := repoToCPE.LoadFromZip(zipR, RHELv2DirName); err != nil {
@@ -245,7 +246,7 @@ func insertRHELv2Vulns(db database.Datastore, r *ziputil.ReadCloser) error {
245246

246247
// This loads application-level vulnerabilities.
247248
// At the moment, this consists of vulnerabilities from NVD and K8s.
248-
func loadApplicationUpdater(cache cache.Cache, manifest *Manifest, zipR *zip.ReadCloser) error {
249+
func loadApplicationUpdater(cache cache.Cache, manifest *Manifest, zipR *zip.Reader) error {
249250
if cache != nil {
250251
updateTime := cache.GetLastUpdate()
251252
if !updateTime.IsZero() && !manifest.Until.After(updateTime) {
@@ -275,7 +276,7 @@ func UpdateFromVulnDump(zipPath string, db database.Datastore, updateInterval ti
275276
defer utils.IgnoreError(zipR.Close)
276277

277278
log.Info("Loading manifest...")
278-
manifest, err := LoadManifestFromDump(zipR)
279+
manifest, err := LoadManifestFromDump(&zipR.Reader)
279280
if err != nil {
280281
return err
281282
}
@@ -287,12 +288,12 @@ func UpdateFromVulnDump(zipPath string, db database.Datastore, updateInterval ti
287288
return errors.Wrap(err, "error beginning vuln loading")
288289
}
289290
if performUpdate {
290-
if err := loadRHELv2Vulns(db, zipR, repoToCPE); err != nil {
291+
if err := loadRHELv2Vulns(db, &zipR.Reader, repoToCPE); err != nil {
291292
_ = finishFn(err)
292293
return errors.Wrap(err, "error loading RHEL vulns")
293294
}
294295

295-
if err := loadOSVulns(zipR, db); err != nil {
296+
if err := loadOSVulns(&zipR.Reader, db); err != nil {
296297
_ = finishFn(err)
297298
return errors.Wrap(err, "error loading OS vulns")
298299
}
@@ -305,7 +306,7 @@ func UpdateFromVulnDump(zipPath string, db database.Datastore, updateInterval ti
305306

306307
errorList := errorhelpers.NewErrorList("loading application-level caches")
307308
for _, appCache := range caches {
308-
if err := loadApplicationUpdater(appCache, manifest, zipR); err != nil {
309+
if err := loadApplicationUpdater(appCache, manifest, &zipR.Reader); err != nil {
309310
errorList.AddError(errors.Wrapf(err, "error loading into in-mem cache %q", appCache.Dir()))
310311
}
311312
}

pkg/ziputil/open.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type ReadCloser struct {
1818
// OpenFile opens the given file in the zip, and returns the ReadCloser.
1919
// It returns an error if the file was not found, or if there was an error opening
2020
// the file.
21-
func OpenFile(zipR *zip.ReadCloser, name string) (*ReadCloser, error) {
21+
func OpenFile(zipR *zip.Reader, name string) (*ReadCloser, error) {
2222
for _, file := range zipR.File {
2323
if file.Name == name {
2424
f, err := file.Open()
@@ -37,7 +37,7 @@ func OpenFile(zipR *zip.ReadCloser, name string) (*ReadCloser, error) {
3737

3838
// OpenFilesInDir opens the files with the given suffix which are in the given dir.
3939
// It returns an error if any of the files cannot be opened.
40-
func OpenFilesInDir(zipR *zip.ReadCloser, dir string, suffix string) ([]*ReadCloser, error) {
40+
func OpenFilesInDir(zipR *zip.Reader, dir string, suffix string) ([]*ReadCloser, error) {
4141
var rs []*ReadCloser
4242
for _, file := range zipR.File {
4343
if within(dir, file.Name) && strings.HasSuffix(file.Name, suffix) {

pkg/ziputil/open_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestOpenFile(t *testing.T) {
2525
defer utils.IgnoreError(zipR.Close)
2626

2727
m := repo2cpe.NewMapping()
28-
assert.NoError(t, m.LoadFromZip(zipR, "rhelv2"))
28+
assert.NoError(t, m.LoadFromZip(&zipR.Reader, "rhelv2"))
2929
}
3030

3131
func TestOpenFilesInDir(t *testing.T) {
@@ -36,7 +36,7 @@ func TestOpenFilesInDir(t *testing.T) {
3636
require.NoError(t, err)
3737
defer utils.IgnoreError(zipR.Close)
3838

39-
rcs, err := ziputil.OpenFilesInDir(zipR, "rhelv2/vulns", ".json")
39+
rcs, err := ziputil.OpenFilesInDir(&zipR.Reader, "rhelv2/vulns", ".json")
4040
assert.NoError(t, err)
4141
assert.Len(t, rcs, 1)
4242

0 commit comments

Comments
 (0)