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

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

Closed
atc0005 opened this issue Nov 10, 2021 · 0 comments · Fixed by #363
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:

config/config.go:234: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) {
					logging.Buffer.Add(logging.LogRecord{
						Level: logrus.ErrorLevel,
						Message: fmt.Sprintf(
							"failed to close file %q: %s",
							*argsConfig.ConfigFile,
							err.Error(),
						),
						Fields: logrus.Fields{"line": logging.GetLineNumber()},
					})
				}
			}
		}()
config/config_test.go:210: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) {
					t.Errorf(
						"failed to close file %q: %s",
						exampleConfigFile,
						err.Error(),
					)
				}
			}
		}()
make: *** [Makefile:140: 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 #362
refs golangci/golangci-lint#2299
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working linting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant