Skip to content

Commit a197a15

Browse files
stevebeattieegibs
andauthored
fix treewide: register more defers immediately (#1355)
v2: revert doing a deferred close() in a loop in rpm.go Signed-off-by: Steve Beattie <steve.beattie@chainguard.dev> Co-authored-by: egibs <20933572+egibs@users.noreply.github.com>
1 parent 42306d1 commit a197a15

File tree

8 files changed

+31
-48
lines changed

8 files changed

+31
-48
lines changed

pkg/action/diff.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
233233
}
234234

235235
srcCh, destCh := make(chan ScanResult, 1), make(chan ScanResult, 1)
236+
237+
defer func() {
238+
close(srcCh)
239+
close(destCh)
240+
}()
241+
236242
srcIsArchive, destIsArchive := programkind.IsSupportedArchive(ctx, srcPath), programkind.IsSupportedArchive(ctx, destPath)
237243
srcResult, destResult := ScanResult{}, ScanResult{}
238244

@@ -373,11 +379,6 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
373379
}
374380
}
375381

376-
defer func() {
377-
close(srcCh)
378-
close(destCh)
379-
}()
380-
381382
return &malcontent.Report{Diff: d}, nil
382383
}
383384

pkg/archive/bz2.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@ func ExtractBz2(ctx context.Context, d, f string) error {
3636
}
3737

3838
buf := archivePool.Get(file.ExtractBuffer) //nolint:nilaway // the buffer pool is created in archive.go
39+
defer archivePool.Put(buf)
3940

4041
tf, err := os.Open(f)
4142
if err != nil {
4243
return fmt.Errorf("failed to open file: %w", err)
4344
}
45+
defer tf.Close()
46+
4447
// Set offset to the file origin regardless of type
4548
_, err = tf.Seek(0, io.SeekStart)
4649
if err != nil {
@@ -64,12 +67,7 @@ func ExtractBz2(ctx context.Context, d, f string) error {
6467
if err != nil {
6568
return fmt.Errorf("failed to create file: %w", err)
6669
}
67-
68-
defer func() {
69-
archivePool.Put(buf)
70-
tf.Close()
71-
out.Close()
72-
}()
70+
defer out.Close()
7371

7472
var written int64
7573
for {

pkg/archive/deb.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,13 @@ func ExtractDeb(ctx context.Context, d, f string) error {
3030
if err != nil {
3131
return fmt.Errorf("failed to open file: %w", err)
3232
}
33+
defer fd.Close()
3334

3435
df, err := deb.Load(fd, f)
3536
if err != nil {
3637
return fmt.Errorf("failed to load file: %w", err)
3738
}
38-
39-
defer func() {
40-
fd.Close()
41-
df.Close()
42-
}()
39+
defer df.Close()
4340

4441
for {
4542
header, err := df.Data.Next()

pkg/archive/gzip.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ func ExtractGzip(ctx context.Context, d string, f string) error {
5858
}
5959

6060
buf := archivePool.Get(file.ExtractBuffer) //nolint:nilaway // the buffer pool is created in archive.go
61+
defer archivePool.Put(buf)
6162

6263
gf, err := os.Open(f)
6364
if err != nil {
6465
return fmt.Errorf("failed to open file: %w", err)
6566
}
67+
defer gf.Close()
6668

6769
base := filepath.Base(f)
6870
target := filepath.Join(d, base[:len(base)-len(filepath.Ext(base))])
@@ -74,18 +76,13 @@ func ExtractGzip(ctx context.Context, d string, f string) error {
7476
if err != nil {
7577
return fmt.Errorf("failed to create gzip reader: %w", err)
7678
}
79+
defer gr.Close()
7780

7881
out, err := os.Create(target)
7982
if err != nil {
8083
return fmt.Errorf("failed to create extracted file: %w", err)
8184
}
82-
83-
defer func() {
84-
archivePool.Put(buf)
85-
gf.Close()
86-
gr.Close()
87-
out.Close()
88-
}()
85+
defer out.Close()
8986

9087
var written int64
9188
for {

pkg/archive/tar.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,14 @@ func ExtractTar(ctx context.Context, d string, f string) error {
4343
}
4444

4545
buf := tarPool.Get(file.ExtractBuffer) //nolint:nilaway // the buffer pool is created in archive.go
46+
defer tarPool.Put(buf)
4647

4748
filename := filepath.Base(f)
4849
tf, err := os.Open(f)
4950
if err != nil {
5051
return fmt.Errorf("failed to open file: %w", err)
5152
}
52-
53-
defer func() {
54-
tarPool.Put(buf)
55-
tf.Close()
56-
}()
53+
defer tf.Close()
5754

5855
isTGZ := strings.Contains(f, ".tar.gz") || strings.Contains(f, ".tgz")
5956
var isGzip bool
@@ -138,6 +135,8 @@ func ExtractTar(ctx context.Context, d string, f string) error {
138135
if err != nil {
139136
return fmt.Errorf("failed to create file: %w", err)
140137
}
138+
defer out.Close()
139+
141140
var written int64
142141
for {
143142
if written > 0 && written%file.ExtractBuffer == 0 && ctx.Err() != nil {

pkg/archive/zip.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ func extractFile(ctx context.Context, zf *zip.File, destDir string, logger *clog
151151
}
152152

153153
buf := zipPool.Get(file.ZipBuffer) //nolint:nilaway // the buffer pool is created in archive.go
154+
defer zipPool.Put(buf)
154155

155156
if err := os.MkdirAll(filepath.Dir(target), 0o700); err != nil {
156157
return fmt.Errorf("failed to create directory structure: %w", err)
@@ -160,17 +161,13 @@ func extractFile(ctx context.Context, zf *zip.File, destDir string, logger *clog
160161
if err != nil {
161162
return fmt.Errorf("failed to open archived file: %w", err)
162163
}
164+
defer src.Close()
163165

164166
dst, err := os.OpenFile(target, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
165167
if err != nil {
166168
return fmt.Errorf("failed to create destination file: %w", err)
167169
}
168-
169-
defer func() {
170-
src.Close()
171-
dst.Close()
172-
zipPool.Put(buf)
173-
}()
170+
defer dst.Close()
174171

175172
var written int64
176173
for {

pkg/archive/zlib.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ func ExtractZlib(ctx context.Context, d string, f string) error {
3535
}
3636

3737
buf := archivePool.Get(file.ExtractBuffer) //nolint:nilaway // the buffer pool is created in archive.go
38+
defer archivePool.Put(buf)
3839

3940
zf, err := os.Open(f)
4041
if err != nil {
4142
return fmt.Errorf("failed to open file: %w", err)
4243
}
44+
defer zf.Close()
4345

4446
base := filepath.Base(f)
4547
target := filepath.Join(d, base[:len(base)-len(filepath.Ext(base))])
@@ -52,18 +54,13 @@ func ExtractZlib(ctx context.Context, d string, f string) error {
5254
if err != nil {
5355
return fmt.Errorf("failed to create zlib reader: %w", err)
5456
}
57+
defer zr.Close()
5558

5659
out, err := os.OpenFile(target, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
5760
if err != nil {
5861
return fmt.Errorf("failed to create extracted file: %w", err)
5962
}
60-
61-
defer func() {
62-
archivePool.Put(buf)
63-
zf.Close()
64-
zr.Close()
65-
out.Close()
66-
}()
63+
defer out.Close()
6764

6865
var written int64
6966
for {

pkg/archive/zstd.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func ExtractZstd(ctx context.Context, d string, f string) error {
3535
}
3636

3737
buf := archivePool.Get(file.ExtractBuffer) //nolint:nilaway // the buffer pool is created in archive.go
38+
defer archivePool.Put(buf)
3839

3940
uncompressed := strings.TrimSuffix(filepath.Base(f), ".zstd")
4041
uncompressed = strings.TrimSuffix(uncompressed, ".zst")
@@ -52,23 +53,19 @@ func ExtractZstd(ctx context.Context, d string, f string) error {
5253
if err != nil {
5354
return fmt.Errorf("failed to create decompressed zstd file: %w", err)
5455
}
56+
defer out.Close()
5557

5658
zstdFile, err := os.Open(f)
5759
if err != nil {
5860
return fmt.Errorf("failed to open zstd file: %w", err)
5961
}
62+
defer zstdFile.Close()
6063

6164
zr, err := zstd.NewReader(zstdFile)
6265
if err != nil {
6366
return fmt.Errorf("failed to open zstd file %s: %w", f, err)
6467
}
65-
66-
defer func() {
67-
archivePool.Put(buf)
68-
zstdFile.Close()
69-
zr.Close()
70-
out.Close()
71-
}()
68+
defer zr.Close()
7269

7370
var written int64
7471
for {

0 commit comments

Comments
 (0)