Skip to content

Commit

Permalink
Wrap tmpRoot removal in a defer instead
Browse files Browse the repository at this point in the history
Signed-off-by: egibs <[email protected]>
  • Loading branch information
egibs committed Dec 17, 2024
1 parent 54591c7 commit a15f3e5
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions pkg/action/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,12 @@ func processArchive(ctx context.Context, c malcontent.Config, rfs []fs.FS, archi
if err != nil {
return nil, fmt.Errorf("extract to temp: %w", err)
}
// Ensure that tmpRoot is removed before returning
defer func() {
if err := os.RemoveAll(tmpRoot); err != nil {
logger.Errorf("remove %s: %v", tmpRoot, err)
}
}()
// macOS will prefix temporary directories with `/private`
// update tmpRoot with this prefix to allow strings.TrimPrefix to work
if runtime.GOOS == "darwin" {
Expand All @@ -511,21 +517,13 @@ func processArchive(ctx context.Context, c malcontent.Config, rfs []fs.FS, archi
for _, extractedFilePath := range extractedPaths {
fr, err := processFile(ctx, c, rfs, extractedFilePath, archivePath, tmpRoot, logger)
if err != nil {
// Ensure we clean up the extracted file path after any error
if err := os.RemoveAll(extractedFilePath); err != nil {
logger.Errorf("remove %s: %v", tmpRoot, err)
}
return nil, err
}
if fr != nil {
// Store a clean reprepsentation of the archive's scanned file to match single file scanning behavior
clean := strings.TrimPrefix(extractedFilePath, tmpRoot)
frs.Store(clean, fr)
}
// Clean up the extracted file path after processing
if err := os.RemoveAll(extractedFilePath); err != nil {
logger.Errorf("remove %s: %v", tmpRoot, err)
}
}
// Remove the temporary parent path after all files are processed to clean up any remaining files
if err := os.RemoveAll(tmpRoot); err != nil {
Expand Down

0 comments on commit a15f3e5

Please sign in to comment.