Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: explicitly ignore errors that we don't need to handle #467

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ linters:
# - dupl
- dupword
- durationcheck
# - errcheck
- errcheck
# - errchkjson
- errname
# - errorlint
Expand Down
28 changes: 14 additions & 14 deletions artifact/image/layerscanning/image/file_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ func TestRead(t *testing.T) {
const bufferSize = 20

tempDir := t.TempDir()
os.WriteFile(path.Join(tempDir, "bar"), []byte("bar"), 0600)
_ = os.WriteFile(path.Join(tempDir, "bar"), []byte("bar"), 0600)

os.WriteFile(path.Join(tempDir, "baz"), []byte("baz"), 0600)
_ = os.WriteFile(path.Join(tempDir, "baz"), []byte("baz"), 0600)
openedRootFile, err := os.OpenFile(path.Join(tempDir, "baz"), os.O_RDONLY, filePermission)
if err != nil {
t.Fatalf("Failed to open file: %v", err)
Expand All @@ -166,8 +166,8 @@ func TestRead(t *testing.T) {
// separate test.
defer openedRootFile.Close()

os.MkdirAll(path.Join(tempDir, "dir1"), 0700)
os.WriteFile(path.Join(tempDir, "dir1/foo"), []byte("foo"), 0600)
_ = os.MkdirAll(path.Join(tempDir, "dir1"), 0700)
_ = os.WriteFile(path.Join(tempDir, "dir1/foo"), []byte("foo"), 0600)

fileNodeWithUnopenedFile := &fileNode{
extractDir: tempDir,
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestRead(t *testing.T) {
}

// Close the file. The Close method is tested in a separate test.
tc.node.Close()
_ = tc.node.Close()
})
}
}
Expand All @@ -264,9 +264,9 @@ func TestReadAt(t *testing.T) {
const bufferSize = 20

tempDir := t.TempDir()
os.WriteFile(path.Join(tempDir, "bar"), []byte("bar"), 0600)
_ = os.WriteFile(path.Join(tempDir, "bar"), []byte("bar"), 0600)

os.WriteFile(path.Join(tempDir, "baz"), []byte("baz"), 0600)
_ = os.WriteFile(path.Join(tempDir, "baz"), []byte("baz"), 0600)
openedRootFile, err := os.OpenFile(path.Join(tempDir, "baz"), os.O_RDONLY, filePermission)
if err != nil {
t.Fatalf("Failed to open file: %v", err)
Expand All @@ -276,8 +276,8 @@ func TestReadAt(t *testing.T) {
// separate test.
defer openedRootFile.Close()

os.MkdirAll(path.Join(tempDir, "dir1"), 0700)
os.WriteFile(path.Join(tempDir, "dir1/foo"), []byte("foo"), 0600)
_ = os.MkdirAll(path.Join(tempDir, "dir1"), 0700)
_ = os.WriteFile(path.Join(tempDir, "dir1/foo"), []byte("foo"), 0600)

fileNodeWithUnopenedFile := &fileNode{
extractDir: tempDir,
Expand Down Expand Up @@ -403,7 +403,7 @@ func TestReadAt(t *testing.T) {
// Test for the Seek method
func TestSeek(t *testing.T) {
tempDir := t.TempDir()
os.WriteFile(path.Join(tempDir, "bar"), []byte("bar"), 0600)
_ = os.WriteFile(path.Join(tempDir, "bar"), []byte("bar"), 0600)

// Test seeking to different positions
tests := []struct {
Expand Down Expand Up @@ -461,7 +461,7 @@ func TestSeek(t *testing.T) {
mode: filePermission,
}
gotPos, err := fileNode.Seek(tc.offset, tc.whence)
fileNode.Close()
_ = fileNode.Close()
if err != nil {
t.Fatalf("Seek failed: %v", err)
}
Expand All @@ -474,7 +474,7 @@ func TestSeek(t *testing.T) {

func TestClose(t *testing.T) {
tempDir := t.TempDir()
os.WriteFile(path.Join(tempDir, "bar"), []byte("bar"), 0600)
_ = os.WriteFile(path.Join(tempDir, "bar"), []byte("bar"), 0600)

fileNodeWithUnopenedFile := &fileNode{
extractDir: tempDir,
Expand Down Expand Up @@ -520,8 +520,8 @@ func TestReadingAfterClose(t *testing.T) {
const readAndCloseEvents = 2

tempDir := t.TempDir()
os.WriteFile(path.Join(tempDir, "bar"), []byte("bar"), 0600)
os.WriteFile(path.Join(tempDir, "baz"), []byte("baz"), 0600)
_ = os.WriteFile(path.Join(tempDir, "bar"), []byte("bar"), 0600)
_ = os.WriteFile(path.Join(tempDir, "baz"), []byte("baz"), 0600)
openedRootFile, err := os.OpenFile(path.Join(tempDir, "baz"), os.O_RDONLY, filePermission)
if err != nil {
t.Fatalf("Failed to open file: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion artifact/image/layerscanning/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ func fillChainLayersWithFileNode(chainLayersToFill []*chainLayer, newNode *fileN

// Add the file to the chain layer. If there is an error, then we fail open.
// TODO: b/379154069 - Add logging for fail open errors.
chainLayer.fileNodeTree.Insert(virtualPath, newNode)
_ = chainLayer.fileNodeTree.Insert(virtualPath, newNode)
}
}

Expand Down
3 changes: 2 additions & 1 deletion artifact/image/layerscanning/image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ func TestFromTarball(t *testing.T) {
t.Fatalf("FromTarball(%v) returned unexpected error: %v", tc.tarPath, gotErr)
}
// Only defer call to CleanUp if the image was created successfully.
//nolint:errcheck
defer gotImage.CleanUp()

chainLayers, err := gotImage.ChainLayers()
Expand Down Expand Up @@ -608,7 +609,7 @@ func TestFromV1Image(t *testing.T) {
gotImage, gotErr := FromV1Image(tc.v1Image, DefaultConfig())
defer func() {
if gotImage != nil {
gotImage.CleanUp()
_ = gotImage.CleanUp()
}
}()

Expand Down
16 changes: 8 additions & 8 deletions artifact/image/layerscanning/image/layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestConvertV1Layer(t *testing.T) {
func TestChainLayerFS(t *testing.T) {
testDir := func() string {
dir := t.TempDir()
os.WriteFile(path.Join(dir, "file1"), []byte("file1"), 0600)
_ = os.WriteFile(path.Join(dir, "file1"), []byte("file1"), 0600)
return dir
}()

Expand All @@ -108,13 +108,13 @@ func TestChainLayerFS(t *testing.T) {

emptyTree := func() *pathtree.Node[fileNode] {
tree := pathtree.NewNode[fileNode]()
tree.Insert("/", root)
_ = tree.Insert("/", root)
return tree
}()
nonEmptyTree := func() *pathtree.Node[fileNode] {
tree := pathtree.NewNode[fileNode]()
tree.Insert("/", root)
tree.Insert("/file1", file1)
_ = tree.Insert("/", root)
_ = tree.Insert("/file1", file1)
return tree
}()

Expand Down Expand Up @@ -150,7 +150,7 @@ func TestChainLayerFS(t *testing.T) {
chainfs := tc.chainLayer.FS()

gotPaths := []string{}
fs.WalkDir(chainfs, "/", func(path string, d fs.DirEntry, err error) error {
_ = fs.WalkDir(chainfs, "/", func(path string, d fs.DirEntry, err error) error {
if err != nil {
t.Errorf("WalkDir(%v) returned error: %v", path, err)
}
Expand Down Expand Up @@ -746,15 +746,15 @@ func setUpChainFS(t *testing.T, maxSymlinkDepth int) (FS, string) {
}

for path, node := range vfsMap {
chainfs.tree.Insert(path, node)
_ = chainfs.tree.Insert(path, node)

if node.IsDir() {
os.MkdirAll(node.RealFilePath(), dirPermission)
_ = os.MkdirAll(node.RealFilePath(), dirPermission)
} else {
if node.mode == fs.ModeSymlink {
continue
}
os.WriteFile(node.RealFilePath(), []byte(path), filePermission)
_ = os.WriteFile(node.RealFilePath(), []byte(path), filePermission)
}
}
return chainfs, tempDir
Expand Down
2 changes: 1 addition & 1 deletion artifact/image/tar/tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestSaveToTarball(t *testing.T) {
defer os.RemoveAll(dir)
path := filepath.Join(dir, "image.tar")
if tc.missingDir {
os.RemoveAll(dir)
_ = os.RemoveAll(dir)
}

err := tar.SaveToTarball(path, tc.image)
Expand Down
6 changes: 3 additions & 3 deletions artifact/image/unpack/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (u *Unpacker) UnpackSquashedFromTarball(dir string, tarPath string) error {
}
log.Infof("Unpacking pass %d of %d", pass+1, u.MaxPass)
requiredTargets, err = unpack(dir, reader, u.SymlinkResolution, u.SymlinkErrStrategy, u.Requirer, requiredTargets, finalPass, u.MaxSizeBytes)
reader.Close()
_ = reader.Close()
if err != nil {
return err
}
Expand Down Expand Up @@ -258,7 +258,7 @@ func (u *Unpacker) UnpackLayers(dir string, image v1.Image) ([]string, error) {
layerDigests = append(layerDigests, digest.String())

layerPath := filepath.Join(dir, strings.Replace(digest.String(), ":", "-", -1))
os.Mkdir(layerPath, fs.ModePerm)
_ = os.Mkdir(layerPath, fs.ModePerm)

// requiredTargets stores targets that symlinks point to.
// This is needed because the symlink may be required by u.requirer, but the target may not be.
Expand Down Expand Up @@ -442,7 +442,7 @@ func unpack(dir string, reader io.Reader, symlinkResolution SymlinkResolution, s
func (u *Unpacker) addSquashedImageDirectory(root string, image v1.Image) error {
squashedImagePath := filepath.Join(root, squashedImageDirectory)

os.Mkdir(squashedImagePath, fs.ModePerm)
_ = os.Mkdir(squashedImagePath, fs.ModePerm)

if err := u.UnpackSquashed(squashedImagePath, image); err != nil {
return fmt.Errorf("failed to unpack all squashed image: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions artifact/image/unpack/unpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestUnpackSquashed(t *testing.T) {
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
os.WriteFile(filepath.Join(dir, "secret.txt"), []byte("some secret\n"), 0644)
_ = os.WriteFile(filepath.Join(dir, "secret.txt"), []byte("some secret\n"), 0644)
return innerDir
}(),
image: mustImageFromPath(t, filepath.Join("testdata", "symlinks.tar")),
Expand Down Expand Up @@ -611,7 +611,7 @@ func mustNewSquashedImage(t *testing.T, pathsToContent map[string]contentAndMode
t.Fatalf("couldn't write %s: %v", path, err)
}
}
w.Close()
_ = w.Close()
layer, err := tarball.LayerFromOpener(func() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewBuffer(buf.Bytes())), nil
})
Expand Down
2 changes: 1 addition & 1 deletion clients/datasource/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestRequestCache(t *testing.T) {
wg.Add(1)
go func() {
t.Helper()
requestCache.Get(i, func() (int, error) {
_, _ = requestCache.Get(i, func() (int, error) {
// Count how many times this function gets called for this key,
// then return the key as the value.
atomic.AddInt32(&fnCalls[i], 1)
Expand Down
14 changes: 7 additions & 7 deletions clients/datasource/http_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (auth *HTTPAuthentication) addDigest(req *http.Request, challenge string) b
return false
}
var b bytes.Buffer
fmt.Fprintf(&b, "%x:%s:%s", ha1, nonce, cnonce)
_, _ = fmt.Fprintf(&b, "%x:%s:%s", ha1, nonce, cnonce)
//nolint:gosec
ha1 = md5.Sum(b.Bytes())
case "MD5":
Expand Down Expand Up @@ -244,23 +244,23 @@ func (auth *HTTPAuthentication) addDigest(req *http.Request, challenge string) b
return false
}
}
fmt.Fprintf(&b, "%x:%s:%s:%s:%s:%x", ha1, nonce, nonceCount, cnonce, "auth", ha2)
_, _ = fmt.Fprintf(&b, "%x:%s:%s:%s:%s:%x", ha1, nonce, nonceCount, cnonce, "auth", ha2)
} else {
fmt.Fprintf(&b, "%x:%s:%x", ha1, nonce, ha2)
_, _ = fmt.Fprintf(&b, "%x:%s:%x", ha1, nonce, ha2)
}
//nolint:gosec
response := md5.Sum(b.Bytes())

var sb strings.Builder
fmt.Fprintf(&sb, "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\"",
_, _ = fmt.Fprintf(&sb, "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\"",
auth.Username, realm, nonce, uri)
if _, ok := params["qop"]; ok {
fmt.Fprintf(&sb, ", qop=auth, nc=%s, cnonce=\"%s\"", nonceCount, cnonce)
_, _ = fmt.Fprintf(&sb, ", qop=auth, nc=%s, cnonce=\"%s\"", nonceCount, cnonce)
}
if alg, ok := params["algorithm"]; ok {
fmt.Fprintf(&sb, ", algorithm=%s", alg)
_, _ = fmt.Fprintf(&sb, ", algorithm=%s", alg)
}
fmt.Fprintf(&sb, ", response=\"%x\", opaque=\"%s\"", response, params["opaque"])
_, _ = fmt.Fprintf(&sb, ", response=\"%x\", opaque=\"%s\"", response, params["opaque"])

req.Header.Add("Authorization", sb.String())

Expand Down
2 changes: 1 addition & 1 deletion common/windows/registry/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (o *OfflineOpener) Open() (Registry, error) {

reg, err := regparser.NewRegistry(f)
if err != nil {
f.Close()
_ = f.Close()
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions detector/weakcredentials/etcshadow/etcshadow.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ func (d Detector) Scan(ctx context.Context, scanRoot *scalibrfs.ScanRoot, ix *in
// Sort users to avoid non-determinism in the processing order from users map.
sort.Strings(problemUsers)
buf := new(strings.Builder)
fmt.Fprintln(buf, "The following users have weak passwords:")
_, _ = fmt.Fprintln(buf, "The following users have weak passwords:")
for _, u := range problemUsers {
fmt.Fprintln(buf, u)
_, _ = fmt.Fprintln(buf, u)
}
problemDescription := buf.String()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func TestExtract(t *testing.T) {

func createFileFromTestData(t *testing.T, root string, subPath string, fileName string, testDataFilePath string) {
t.Helper()
os.MkdirAll(filepath.Join(root, subPath), 0755)
_ = os.MkdirAll(filepath.Join(root, subPath), 0755)
testData, err := os.ReadFile(testDataFilePath)
if err != nil {
t.Fatalf("read from %s: %v\n", testDataFilePath, err)
Expand Down
2 changes: 1 addition & 1 deletion extractor/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ func (i *ScanInput) GetRealPath() (string, error) {
return "", err
}
defer f.Close()
io.Copy(f, i.Reader)
_, _ = io.Copy(f, i.Reader)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this error should not be ignored

return path, nil
}

Expand Down
2 changes: 1 addition & 1 deletion extractor/filesystem/internal/walkdir_iterate.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func readDir(fsys scalibrfs.FS, name string) (*dirIterator, error) {
if !ok {
// Fallback if ReadDirFile is not implemented: Use fs.DirFS's ReadDir().
// (Uses more memory since it reads all subdirs at once.)
file.Close()
_ = file.Close()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be ignored.

files, err := fsys.ReadDir(name)
if err != nil {
return nil, &fs.PathError{Op: "readdir", Path: name, Err: errors.New("not implemented")}
Expand Down
2 changes: 2 additions & 0 deletions extractor/filesystem/internal/walkdir_iterate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func TestWalkDir(t *testing.T) {
if err = os.Chdir(tmpDir); err != nil {
t.Fatal("entering temp dir:", err)
}
//nolint:errcheck
defer os.Chdir(origDir)

fsys := makeTree()
Expand Down Expand Up @@ -147,6 +148,7 @@ func TestIssue51617(t *testing.T) {
if err := os.Chmod(bad, 0); err != nil {
t.Fatal(err)
}
//nolint:errcheck
defer os.Chmod(bad, 0700) // avoid errors on cleanup
var saw []string
err := WalkDirUnsorted(scalibrfs.DirFS(dir), ".", func(path string, d fs.DirEntry, err error) error {
Expand Down
1 change: 1 addition & 0 deletions extractor/filesystem/language/java/archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ func mustJar(t *testing.T, path string) *os.File {
if err != nil {
t.Fatalf("os.CreateTemp(\"temp-*.jar\") unexpected error: %v", err)
}
//nolint:errcheck
defer jarFile.Sync()

zipWriter := zip.NewWriter(jarFile)
Expand Down
4 changes: 2 additions & 2 deletions extractor/filesystem/language/java/archive/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ func NewOmitEmptyLinesReader(r io.Reader) io.Reader {
for scanner.Scan() {
line := scanner.Text()
if line != "" {
pw.Write([]byte(line + "\n"))
_, _ = pw.Write([]byte(line + "\n"))
}
}
if err := scanner.Err(); err != nil {
pw.CloseWithError(err)
_ = pw.CloseWithError(err)
}
}()

Expand Down
2 changes: 1 addition & 1 deletion extractor/filesystem/os/apk/apk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func getInventory(path, pkgName, origin, version, osID, osVersionID, maintainer,

func createOsRelease(t *testing.T, root string, content string) {
t.Helper()
os.MkdirAll(filepath.Join(root, "etc"), 0755)
_ = os.MkdirAll(filepath.Join(root, "etc"), 0755)
err := os.WriteFile(filepath.Join(root, "etc/os-release"), []byte(content), 0644)
if err != nil {
t.Fatalf("write to %s: %v\n", filepath.Join(root, "etc/os-release"), err)
Expand Down
2 changes: 1 addition & 1 deletion extractor/filesystem/os/cos/cos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func TestToPURL(t *testing.T) {

func createOsRelease(t *testing.T, root string, content string) {
t.Helper()
os.MkdirAll(filepath.Join(root, "etc"), 0755)
_ = os.MkdirAll(filepath.Join(root, "etc"), 0755)
err := os.WriteFile(filepath.Join(root, "etc/os-release"), []byte(content), 0644)
if err != nil {
t.Fatalf("write to %s: %v\n", filepath.Join(root, "etc/os-release"), err)
Expand Down
2 changes: 1 addition & 1 deletion extractor/filesystem/os/dpkg/dpkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,7 @@ func TestEcosystem(t *testing.T) {

func createOsRelease(t *testing.T, root string, content string) {
t.Helper()
os.MkdirAll(filepath.Join(root, "etc"), 0755)
_ = os.MkdirAll(filepath.Join(root, "etc"), 0755)
err := os.WriteFile(filepath.Join(root, "etc/os-release"), []byte(content), 0644)
if err != nil {
t.Fatalf("write to %s: %v\n", filepath.Join(root, "etc/os-release"), err)
Expand Down
Loading