Skip to content

Commit

Permalink
Implement fixes, ignore G115 for header mode
Browse files Browse the repository at this point in the history
Signed-off-by: egibs <[email protected]>
  • Loading branch information
egibs committed Oct 7, 2024
1 parent ea1d80f commit acd0eaf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ linters:
enable:
- asciicheck
- bodyclose
- copyloopvar
- cyclop
- dogsled
- dupl
Expand All @@ -108,7 +109,6 @@ linters:
- errname
- errorlint
- exhaustive
- exportloopref
- forcetypeassert
- gocognit
- goconst
Expand Down
17 changes: 14 additions & 3 deletions pkg/action/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"io/fs"
"math"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -72,9 +73,19 @@ func extractTar(ctx context.Context, d string, f string) error {

for {
header, err := tr.Next()
if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF) {

mode := func(v int64) (uint32, bool) {
if v > math.MaxUint32 {
return math.MaxUint32, false
}
return uint32(v), true // #nosec G115
}

hm, ok := mode(header.Mode)
if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF) || !ok {
break
}

if err != nil {
return fmt.Errorf("failed to read tar header: %w", err)
}
Expand All @@ -84,7 +95,7 @@ func extractTar(ctx context.Context, d string, f string) error {
}
target := filepath.Join(d, clean)
if header.FileInfo().IsDir() {
if err := os.MkdirAll(target, os.FileMode(header.Mode)); err != nil {
if err := os.MkdirAll(target, os.FileMode(hm)); err != nil { // #nosec G115
return fmt.Errorf("failed to create directory: %w", err)
}
continue
Expand All @@ -94,7 +105,7 @@ func extractTar(ctx context.Context, d string, f string) error {
return fmt.Errorf("failed to create directory for file: %w", err)
}

f, err := os.OpenFile(target, os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.FileMode(header.Mode))
f, err := os.OpenFile(target, os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.FileMode(hm)) // #nosec G115
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/action/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func TestExtractionMethod(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := extractionMethod(tt.ext)
if (got == nil) != (tt.want == nil) {
Expand Down Expand Up @@ -72,7 +71,6 @@ func TestExtractionMultiple(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.path, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
Expand Down
1 change: 0 additions & 1 deletion pkg/action/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ func recursiveScan(ctx context.Context, c malcontent.Config) (*malcontent.Report
var g errgroup.Group
g.SetLimit(maxConcurrency)
for path := range pc {
path := path
g.Go(func() error {
if isSupportedArchive(path) {
return handleArchive(path)
Expand Down

0 comments on commit acd0eaf

Please sign in to comment.