Skip to content

Commit ec2a923

Browse files
authored
Bump golangci-lint to v2.6.0; use exec.CommandContext for UPX command(s) (#1197)
Signed-off-by: egibs <[email protected]>
1 parent 9b01346 commit ec2a923

File tree

10 files changed

+18
-17
lines changed

10 files changed

+18
-17
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ LINTERS :=
4444
FIXERS :=
4545

4646
GOLANGCI_LINT_CONFIG := $(LINT_ROOT)/.golangci.yml
47-
GOLANGCI_LINT_VERSION ?= v2.1.6
47+
GOLANGCI_LINT_VERSION ?= v2.6.0
4848
GOLANGCI_LINT_BIN := $(LINT_ROOT)/out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
4949
$(GOLANGCI_LINT_BIN):
5050
mkdir -p $(LINT_ROOT)/out/linters

pkg/action/diff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
230230

231231
srcCh, destCh := make(chan ScanResult, 1), make(chan ScanResult, 1)
232232

233-
srcIsArchive, destIsArchive := programkind.IsSupportedArchive(srcPath), programkind.IsSupportedArchive(destPath)
233+
srcIsArchive, destIsArchive := programkind.IsSupportedArchive(ctx, srcPath), programkind.IsSupportedArchive(ctx, destPath)
234234

235235
g.Go(func() error {
236236
files, base, err := relFileReport(ctx, c, srcPath, isImage)

pkg/action/scan.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func scanSinglePath(ctx context.Context, c malcontent.Config, path string, ruleF
126126
}
127127

128128
mime := "<unknown>"
129-
kind, err := programkind.File(path)
129+
kind, err := programkind.File(ctx, path)
130130
if err != nil && !interactive(c) {
131131
logger.Errorf("file type failure: %s: %s", path, err)
132132
}
@@ -523,7 +523,7 @@ func processPath(ctx context.Context, path string, scanInfo scanPathInfo, c malc
523523
case <-ctx.Done():
524524
return ctx.Err()
525525
default:
526-
if programkind.IsSupportedArchive(path) {
526+
if programkind.IsSupportedArchive(ctx, path) {
527527
return handleArchiveFile(ctx, path, c, r, matchChan, matchOnce, logger)
528528
}
529529
return handleSingleFile(ctx, path, scanInfo, c, r, matchChan, matchOnce, logger)

pkg/archive/archive.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func extractNestedArchive(ctx context.Context, c malcontent.Config, d string, f
5757
}
5858

5959
isArchive := false
60-
ft, err := programkind.File(fullPath)
60+
ft, err := programkind.File(ctx, fullPath)
6161
if err != nil {
6262
return fmt.Errorf("failed to determine file type: %w", err)
6363
}
@@ -156,7 +156,7 @@ func ExtractArchiveToTempDir(ctx context.Context, c malcontent.Config, path stri
156156

157157
var extract func(context.Context, string, string) error
158158
// Check for zlib-compressed files first and use the zlib-specific function
159-
ft, err := programkind.File(path)
159+
ft, err := programkind.File(ctx, path)
160160
if err != nil {
161161
return "", fmt.Errorf("failed to determine file type: %w", err)
162162
}

pkg/archive/gzip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func ExtractGzip(ctx context.Context, d string, f string) error {
3131

3232
// Check whether the provided file is a valid gzip archive
3333
var isGzip bool
34-
if ft, err := programkind.File(f); err == nil && ft != nil {
34+
if ft, err := programkind.File(ctx, f); err == nil && ft != nil {
3535
if _, ok := GzMIME[ft.MIME]; ok {
3636
isGzip = true
3737
}

pkg/archive/tar.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func ExtractTar(ctx context.Context, d string, f string) error {
6363

6464
isTGZ := strings.Contains(f, ".tar.gz") || strings.Contains(f, ".tgz")
6565
var isGzip bool
66-
if ft, err := programkind.File(f); err == nil && ft != nil {
66+
if ft, err := programkind.File(ctx, f); err == nil && ft != nil {
6767
if _, ok := GzMIME[ft.MIME]; ok {
6868
isGzip = true
6969
}

pkg/archive/upx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func ExtractUPX(ctx context.Context, d, f string) error {
5252
return fmt.Errorf("failed to write file: %w", err)
5353
}
5454

55-
cmd := exec.Command("upx", "-d", "-k", target)
55+
cmd := exec.CommandContext(ctx, "upx", "-d", "-k", target)
5656
output, err := cmd.CombinedOutput()
5757
if err != nil {
5858
os.Remove(target)

pkg/archive/zip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func ExtractZip(ctx context.Context, d string, f string) error {
5252
}
5353

5454
var isZip bool
55-
if ft, err := programkind.File(f); err == nil && ft != nil {
55+
if ft, err := programkind.File(ctx, f); err == nil && ft != nil {
5656
if _, ok := zipMIME[ft.MIME]; ok {
5757
isZip = true
5858
}

pkg/programkind/programkind.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package programkind
55

66
import (
77
"bytes"
8+
"context"
89
"errors"
910
"fmt"
1011
"io/fs"
@@ -123,11 +124,11 @@ const headerSize int = 512
123124

124125
// IsSupportedArchive returns whether a path can be processed by our archive extractor.
125126
// UPX files are an edge case since they may or may not even have an extension that can be referenced.
126-
func IsSupportedArchive(path string) bool {
127+
func IsSupportedArchive(ctx context.Context, path string) bool {
127128
if _, isValidArchive := ArchiveMap[GetExt(path)]; isValidArchive {
128129
return true
129130
}
130-
if ft, err := File(path); err == nil && ft != nil {
131+
if ft, err := File(ctx, path); err == nil && ft != nil {
131132
if ft.MIME == "application/x-upx" {
132133
return true
133134
}
@@ -179,7 +180,7 @@ func UPXInstalled() error {
179180
}
180181

181182
// IsValidUPX checks whether a suspected UPX-compressed file can be decompressed with UPX.
182-
func IsValidUPX(header []byte, path string) (bool, error) {
183+
func IsValidUPX(ctx context.Context, header []byte, path string) (bool, error) {
183184
if !bytes.Contains(header, []byte("UPX!")) {
184185
return false, nil
185186
}
@@ -188,7 +189,7 @@ func IsValidUPX(header []byte, path string) (bool, error) {
188189
return false, err
189190
}
190191

191-
cmd := exec.Command("upx", "-l", "-f", path)
192+
cmd := exec.CommandContext(ctx, "upx", "-l", "-f", path)
192193
output, err := cmd.CombinedOutput()
193194

194195
if err != nil && (bytes.Contains(output, []byte("NotPackedException")) ||
@@ -240,7 +241,7 @@ func makeFileType(path string, ext string, mime string) *FileType {
240241
}
241242

242243
// File detects what kind of program this file might be.
243-
func File(path string) (*FileType, error) {
244+
func File(ctx context.Context, path string) (*FileType, error) {
244245
// Follow symlinks and return cleanly if the target does not exist
245246
_, err := filepath.EvalSymlinks(path)
246247
if os.IsNotExist(err) {
@@ -292,7 +293,7 @@ func File(path string) (*FileType, error) {
292293
}
293294

294295
// final strategy: DIY matching where mimetype is too strict.
295-
if isUPX, err := IsValidUPX(hdr, path); err == nil && isUPX {
296+
if isUPX, err := IsValidUPX(ctx, hdr, path); err == nil && isUPX {
296297
return Path(".upx"), nil
297298
}
298299

pkg/programkind/programkind_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestFile(t *testing.T) {
2727
for _, tt := range tests {
2828
t.Run(tt.in, func(t *testing.T) {
2929
t.Parallel()
30-
got, err := File(filepath.Join("testdata/", tt.in))
30+
got, err := File(t.Context(), filepath.Join("testdata/", tt.in))
3131
if err != nil {
3232
t.Errorf("File(%s) returned error: %v", tt.in, err)
3333
}

0 commit comments

Comments
 (0)