Skip to content
This repository was archived by the owner on Feb 15, 2024. It is now read-only.
This repository was archived by the owner on Feb 15, 2024. It is now read-only.

False positive G307: Deferring unsafe method "Close" on type "*os.File" (gosec) linting error #261

@atc0005

Description

@atc0005

This linting error was surfaced from an upgraded golangci-lint:

internal/fileutils/contains.go:56:2: G307: Deferring unsafe method "Close" on type "*os.File" (gosec)
	defer func() {
		if err := f.Close(); err != nil {
			// Ignore "file already closed" errors
			if !errors.Is(err, os.ErrClosed) {
				log.Errorf(
					"%s: failed to close file %q: %s",
					myFuncName,
					filename,
					err.Error(),
				)
			}
		}
	}()
internal/files/process.go:596:2: G307: Deferring unsafe method "Close" on type "*os.File" (gosec)
	defer func(filename string) {
		if err := f.Close(); err != nil {
			// Ignore "file already closed" errors
			if !errors.Is(err, os.ErrClosed) {
				log.Errorf(
					"%s: failed to close file %q: %s",
					myFuncName,
					filename,
					err.Error(),
				)
			}
		}
	}(filename)
internal/config/config.go:249:3: G307: Deferring unsafe method "Close" on type "*os.File" (gosec)
		defer func() {
			if err := fh.Close(); err != nil {
				// Ignore "file already closed" errors
				if !errors.Is(err, os.ErrClosed) {
					log.Errorf(
						"%s: failed to close file %q: %s",
						myFuncName,
						err.Error(),
					)
				}
			}
		}()
make: *** [Makefile:122: linting] Error 1
Error: Process completed with exit code 2.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinglinting

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions