Skip to content

Commit cca06d6

Browse files
stevebeattieegibs
andcommitted
fix treewide: register more deferes immediately
Signed-off-by: Steve Beattie <steve.beattie@chainguard.dev> Co-authored-by: egibs <20933572+egibs@users.noreply.github.com>
1 parent f9716b0 commit cca06d6

10 files changed

Lines changed: 69 additions & 52 deletions

File tree

pkg/action/diff.go

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

241241
srcCh, destCh := make(chan ScanResult, 1), make(chan ScanResult, 1)
242+
243+
defer func() {
244+
close(srcCh)
245+
close(destCh)
246+
}()
247+
242248
srcIsArchive, destIsArchive := programkind.IsSupportedArchive(ctx, srcPath), programkind.IsSupportedArchive(ctx, destPath)
243249
srcResult, destResult := ScanResult{}, ScanResult{}
244250

@@ -379,11 +385,6 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
379385
}
380386
}
381387

382-
defer func() {
383-
close(srcCh)
384-
close(destCh)
385-
}()
386-
387388
return &malcontent.Report{Diff: d}, nil
388389
}
389390

pkg/archive/bz2.go

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

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

3738
tf, err := os.Open(f)
3839
if err != nil {
3940
return fmt.Errorf("failed to open file: %w", err)
4041
}
42+
defer tf.Close()
43+
4144
// Set offset to the file origin regardless of type
4245
_, err = tf.Seek(0, io.SeekStart)
4346
if err != nil {
@@ -61,12 +64,7 @@ func ExtractBz2(ctx context.Context, d, f string) error {
6164
if err != nil {
6265
return fmt.Errorf("failed to create file: %w", err)
6366
}
64-
65-
defer func() {
66-
archivePool.Put(buf)
67-
tf.Close()
68-
out.Close()
69-
}()
67+
defer out.Close()
7068

7169
var written int64
7270
for {

pkg/archive/deb.go

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

3738
df, err := deb.Load(fd, f)
3839
if err != nil {
3940
return fmt.Errorf("failed to load file: %w", err)
4041
}
41-
42-
defer func() {
43-
fd.Close()
44-
df.Close()
45-
}()
42+
defer df.Close()
4643

4744
for {
4845
header, err := df.Data.Next()

pkg/archive/gzip.go

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

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

5960
gf, err := os.Open(f)
6061
if err != nil {
6162
return fmt.Errorf("failed to open file: %w", err)
6263
}
64+
defer gf.Close()
6365

6466
base := filepath.Base(f)
6567
target := filepath.Join(d, base[:len(base)-len(filepath.Ext(base))])
@@ -71,18 +73,13 @@ func ExtractGzip(ctx context.Context, d string, f string) error {
7173
if err != nil {
7274
return fmt.Errorf("failed to create gzip reader: %w", err)
7375
}
76+
defer gr.Close()
7477

7578
out, err := os.Create(target)
7679
if err != nil {
7780
return fmt.Errorf("failed to create extracted file: %w", err)
7881
}
79-
80-
defer func() {
81-
archivePool.Put(buf)
82-
gf.Close()
83-
gr.Close()
84-
out.Close()
85-
}()
82+
defer out.Close()
8683

8784
var written int64
8885
for {

pkg/archive/rpm.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func ExtractRPM(ctx context.Context, d, f string) error {
147147
if err != nil {
148148
return fmt.Errorf("failed to create file: %w", err)
149149
}
150+
defer out.Close()
150151

151152
var written int64
152153
for {
@@ -173,10 +174,6 @@ func ExtractRPM(ctx context.Context, d, f string) error {
173174
return fmt.Errorf("failed to read file contents: %w", err)
174175
}
175176
}
176-
177-
if err := out.Close(); err != nil {
178-
return fmt.Errorf("failed to close file: %w", err)
179-
}
180177
}
181178

182179
return nil

pkg/archive/tar.go

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

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

5455
filename := filepath.Base(f)
5556
tf, err := os.Open(f)
5657
if err != nil {
5758
return fmt.Errorf("failed to open file: %w", err)
5859
}
59-
60-
defer func() {
61-
tarPool.Put(buf)
62-
tf.Close()
63-
}()
60+
defer tf.Close()
6461

6562
isTGZ := strings.Contains(f, ".tar.gz") || strings.Contains(f, ".tgz")
6663
var isGzip bool
@@ -145,6 +142,8 @@ func ExtractTar(ctx context.Context, d string, f string) error {
145142
if err != nil {
146143
return fmt.Errorf("failed to create file: %w", err)
147144
}
145+
defer out.Close()
146+
148147
var written int64
149148
for {
150149
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
@@ -156,6 +156,7 @@ func extractFile(ctx context.Context, zf *zip.File, destDir string, logger *clog
156156
}
157157

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

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

169171
dst, err := os.OpenFile(target, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
170172
if err != nil {
171173
return fmt.Errorf("failed to create destination file: %w", err)
172174
}
173-
174-
defer func() {
175-
src.Close()
176-
dst.Close()
177-
zipPool.Put(buf)
178-
}()
175+
defer dst.Close()
179176

180177
var written int64
181178
for {

pkg/archive/zlib.go

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

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

3637
zf, err := os.Open(f)
3738
if err != nil {
3839
return fmt.Errorf("failed to open file: %w", err)
3940
}
41+
defer zf.Close()
4042

4143
base := filepath.Base(f)
4244
target := filepath.Join(d, base[:len(base)-len(filepath.Ext(base))])
@@ -49,18 +51,13 @@ func ExtractZlib(ctx context.Context, d string, f string) error {
4951
if err != nil {
5052
return fmt.Errorf("failed to create zlib reader: %w", err)
5153
}
54+
defer zr.Close()
5255

5356
out, err := os.OpenFile(target, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
5457
if err != nil {
5558
return fmt.Errorf("failed to create extracted file: %w", err)
5659
}
57-
58-
defer func() {
59-
archivePool.Put(buf)
60-
zf.Close()
61-
zr.Close()
62-
out.Close()
63-
}()
60+
defer out.Close()
6461

6562
var written int64
6663
for {

pkg/archive/zlib.go.rej

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--- pkg/archive/zlib.go
2+
+++ pkg/archive/zlib.go
3+
@@ -35,11 +35,13 @@ func ExtractZlib(ctx context.Context, d string, f string) error {
4+
}
5+
6+
buf := archivePool.Get(file.ExtractBuffer) //nolint:nilaway // the buffer pool is created in archive.go
7+
+ defer archivePool.Put(buf)
8+
9+
zf, err := os.Open(f)
10+
if err != nil {
11+
return fmt.Errorf("failed to open file: %w", err)
12+
}
13+
+ defer zf.Close()
14+
15+
base := filepath.Base(f)
16+
target := filepath.Join(d, base[:len(base)-len(filepath.Ext(base))])
17+
@@ -52,18 +54,13 @@ func ExtractZlib(ctx context.Context, d string, f string) error {
18+
if err != nil {
19+
return fmt.Errorf("failed to create zlib reader: %w", err)
20+
}
21+
+ defer zr.Close()
22+
23+
out, err := os.OpenFile(target, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
24+
if err != nil {
25+
return fmt.Errorf("failed to create extracted file: %w", err)
26+
}
27+
-
28+
- defer func() {
29+
- archivePool.Put(buf)
30+
- zf.Close()
31+
- zr.Close()
32+
- out.Close()
33+
- }()
34+
+ defer out.Close()
35+
36+
var written int64
37+
for {

pkg/archive/zstd.go

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

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

3637
uncompressed := strings.TrimSuffix(filepath.Base(f), ".zstd")
3738
uncompressed = strings.TrimSuffix(uncompressed, ".zst")
@@ -49,23 +50,19 @@ func ExtractZstd(ctx context.Context, d string, f string) error {
4950
if err != nil {
5051
return fmt.Errorf("failed to create decompressed zstd file: %w", err)
5152
}
53+
defer out.Close()
5254

5355
zstdFile, err := os.Open(f)
5456
if err != nil {
5557
return fmt.Errorf("failed to open zstd file: %w", err)
5658
}
59+
defer zstdFile.Close()
5760

5861
zr, err := zstd.NewReader(zstdFile)
5962
if err != nil {
6063
return fmt.Errorf("failed to open zstd file %s: %w", f, err)
6164
}
62-
63-
defer func() {
64-
archivePool.Put(buf)
65-
zstdFile.Close()
66-
zr.Close()
67-
out.Close()
68-
}()
65+
defer zr.Close()
6966

7067
var written int64
7168
for {

0 commit comments

Comments
 (0)