Skip to content
This repository has been 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

Closed
atc0005 opened this issue Nov 10, 2021 · 0 comments · Fixed by #262
Closed
Assignees
Labels
bug Something isn't working linting
Milestone

Comments

@atc0005
Copy link
Owner

atc0005 commented Nov 10, 2021

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.
@atc0005 atc0005 added bug Something isn't working linting labels Nov 10, 2021
@atc0005 atc0005 added this to the Next Release milestone Nov 10, 2021
@atc0005 atc0005 self-assigned this Nov 10, 2021
atc0005 added a commit that referenced this issue Nov 10, 2021
Issues reported after upgrading golangci-lint to v1.43.0.
gosec was updated in that version from v2.8.1 to v2.9.1.

refs #261
refs golangci/golangci-lint#2299
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working linting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant