Skip to content

Commit 2bf11c5

Browse files
authored
chore: update context in mal.go; use errors.Is for ErrNotExist checks (#1359)
Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
1 parent b5483b2 commit 2bf11c5

6 files changed

Lines changed: 25 additions & 19 deletions

File tree

cmd/mal/mal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func main() {
121121
ver string
122122
)
123123

124-
ctx, cancel := context.WithCancel(context.TODO())
124+
ctx, cancel := context.WithCancel(context.Background())
125125
ctx = clog.WithLogger(ctx, log)
126126
defer cancel()
127127

pkg/action/path.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package action
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"io/fs"
1011
"os"
@@ -28,7 +29,7 @@ func findFilesRecursively(ctx context.Context, rootPath string) ([]string, error
2829
if err != nil {
2930
// If the target does not exist, log the error but return gracefully
3031
// This is useful when scanning -compat packages
31-
if os.IsNotExist(err) {
32+
if errors.Is(err, fs.ErrNotExist) {
3233
logger.Debugf("symlink target does not exist: %s", err.Error())
3334
return nil, nil
3435
}

pkg/archive/archive.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ package archive
66
import (
77
"archive/tar"
88
"context"
9+
"errors"
910
"fmt"
1011
"io"
12+
"io/fs"
1113
"os"
1214
"path/filepath"
1315
"runtime"
@@ -83,7 +85,7 @@ func extractNestedArchive(ctx context.Context, c malcontent.Config, d string, f
8385

8486
fullPath := filepath.Join(d, f)
8587
fi, err := os.Stat(fullPath)
86-
if os.IsNotExist(err) {
88+
if errors.Is(err, fs.ErrNotExist) {
8789
return nil
8890
}
8991
if err != nil {
@@ -397,7 +399,7 @@ func handleHardlink(dir, linkPath, linkTarget string) error {
397399
}
398400

399401
if err := os.Link(targetPath, fullPath); err != nil {
400-
if os.IsNotExist(err) {
402+
if errors.Is(err, fs.ErrNotExist) {
401403
return nil
402404
}
403405
return fmt.Errorf("failed to create hardlink: %w", err)

pkg/compile/compile_test.go

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

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"io/fs"
1011
"os"
@@ -37,7 +38,7 @@ func clearRulesCache(t *testing.T, fss []fs.FS) {
3738

3839
cacheFile := filepath.Join(cacheDir, fmt.Sprintf("rules-%s.cache", hash))
3940

40-
if err := os.Remove(cacheFile); err != nil && !os.IsNotExist(err) {
41+
if err := os.Remove(cacheFile); err != nil && !errors.Is(err, fs.ErrNotExist) {
4142
t.Fatalf("Failed to remove cache file: %v", err)
4243
}
4344
}
@@ -59,7 +60,7 @@ func clearRulesCacheB(b *testing.B, fss []fs.FS) {
5960

6061
cacheFile := filepath.Join(cacheDir, fmt.Sprintf("rules-%s.cache", hash))
6162

62-
if err := os.Remove(cacheFile); err != nil && !os.IsNotExist(err) {
63+
if err := os.Remove(cacheFile); err != nil && !errors.Is(err, fs.ErrNotExist) {
6364
b.Fatalf("Failed to remove cache file: %v", err)
6465
}
6566
}
@@ -122,7 +123,7 @@ func TestCacheOperations(t *testing.T) {
122123
t.Fatalf("Failed to save rules to cache: %v", err)
123124
}
124125

125-
if _, err := os.Stat(cacheFile); os.IsNotExist(err) {
126+
if _, err := os.Stat(cacheFile); errors.Is(err, fs.ErrNotExist) {
126127
t.Fatal("Cache file was not created")
127128
}
128129

pkg/programkind/programkind.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func UPXInstalled() error {
237237

238238
fi, err := os.Stat(upxPath)
239239
if err != nil {
240-
if os.IsNotExist(err) {
240+
if errors.Is(err, fs.ErrNotExist) {
241241
return ErrUPXNotFound
242242
}
243243
return fmt.Errorf("failed to check for UPX executable: %w", err)
@@ -387,7 +387,7 @@ func containsValue(value string, slice []string) bool {
387387
func File(ctx context.Context, path string) (*FileType, error) {
388388
// Follow symlinks and return cleanly if the target does not exist
389389
_, err := filepath.EvalSymlinks(path)
390-
if os.IsNotExist(err) {
390+
if errors.Is(err, fs.ErrNotExist) {
391391
return nil, nil
392392
}
393393

third_party/yara/update.sh

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,40 +51,42 @@ function fixup_rules() {
5151
# update_dep updates a dependency to the latest release
5252
function update_dep() {
5353
local kind=$1
54-
local tmpdir=$(mktemp -d)
54+
local tmpdir=""
5555
local rel="unknown"
5656

57+
tmpdir="$(mktemp -d)"
58+
5759
mkdir -p "${kind}" || true
5860

5961
case $kind in
6062
YARAForge)
6163
rel=$(latest_github_release YARAHQ/yara-forge)
62-
curl -L -o "${tmpdir}/yaraforge.zip" "https://github.com/YARAHQ/yara-forge/releases/download/${rel}/yara-forge-rules-full.zip"
63-
unzip -o -j "${tmpdir}/yaraforge.zip" packages/full/yara-rules-full.yar -d "${kind}"
64+
curl -L -o "${tmpdir}"/yaraforge.zip "https://github.com/YARAHQ/yara-forge/releases/download/${rel}/yara-forge-rules-full.zip"
65+
unzip -o -j "${tmpdir}"/yaraforge.zip packages/full/yara-rules-full.yar -d "${kind}"
6466
;;
6567
huntress)
6668
rel=$(git_clone https://github.com/huntresslabs/threat-intel.git "${tmpdir}")
6769
find "${tmpdir}" \( -name "*.yar*" -o -name "*LICENSE*" \) -print -exec cp {} "${kind}" \;
6870
# error: rule "BOINC" in boinc.yar(1): syntax error, unexpected identifier, expecting '{'
69-
rm "${kind}/boinc.yar"
71+
rm "${kind}"/boinc.yar
7072
# ^ expecting pattern modifier, pattern identifier or `condition`, found `}` (missing condition field)
71-
rm "${kind}/defendnot_tool.yar"
73+
rm "${kind}"/defendnot_tool.yar
7274
;;
7375
InQuest-VT)
7476
rel=$(git_clone https://github.com/InQuest/yara-rules-vt.git "${tmpdir}")
7577
find "${tmpdir}" \( -name "*.yar*" -o -name "*LICENSE*" -o -name "README*" \) -print -exec cp {} "${kind}" \;
7678
;;
7779
bartblaze)
7880
rel=$(git_clone https://github.com/bartblaze/Yara-rules.git "${tmpdir}")
79-
cp -Rp ${tmpdir}/LICENSE ${tmpdir}/README.md ${tmpdir}/rules/* "${kind}/"
81+
cp -Rp "${tmpdir}"/LICENSE "${tmpdir}"/README.md "${tmpdir}"/rules/* "${kind}"/
8082
;;
8183
JPCERT)
8284
rel=$(git_clone https://github.com/JPCERTCC/jpcert-yara.git "${tmpdir}")
8385
find "${tmpdir}" \( -name "*.yar*" -o -name "*LICENSE*" -o -name "README*" \) -print -exec cp {} "${kind}" \;
8486
;;
8587
TTC-CERT)
8688
rel=$(git_clone https://github.com/ttc-cert/TTC-CERT-YARA-Rules.git "${tmpdir}")
87-
cp -Rp ${tmpdir}/* "${kind}/"
89+
cp -Rp "${tmpdir}"/* "${kind}"/
8890
;;
8991
elastic)
9092
rel=$(git_clone https://github.com/elastic/protections-artifacts.git "${tmpdir}")
@@ -96,12 +98,12 @@ function update_dep() {
9698
;;
9799
esac
98100

99-
fixup_rules ${kind}/*.yar* # nolint
100-
echo "${rel}" >"${kind}/RELEASE"
101+
fixup_rules "${kind}"/*.yar*
102+
echo "${rel}" > "${kind}"/RELEASE
101103
echo "updated ${kind} to ${rel}"
102104
}
103105

104-
cd "$(dirname $0)"
106+
cd "$(dirname "$0")"
105107

106108
if [[ "$1" != "" ]]; then
107109
update_dep "$1"

0 commit comments

Comments
 (0)