Skip to content

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

@atc0005

Description

@atc0005

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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinglinting

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions