-
-
Notifications
You must be signed in to change notification settings - Fork 619
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
G307: Unsafe defer call of a method returning an error for defer file.Close()
?
#512
Comments
This is because https://golang.org/pkg/os/#File.Close returns and error which is not checked when calling defer. This was recently introduced to catch this kind of situation which could lead to a crash. We should maybe rethink this rule since is a common pattern which might generate more headaches than catching security issues. |
@ccojocar hi! Thanks for reply. I think, better way to make it works, is skip
|
This is a solution. You can use this code snippet to avoid the warning: defer func() {
if err := file.Close(); err != nil {
logger.Printf("Error closing file: %s\n", err)
}
}() |
Thanks, that works! 👍 |
``` G307 (CWE-703): Deferring unsafe method "Close" on type "*os.File" ``` Solution from: securego/gosec#512
``` G307 (CWE-703): Deferring unsafe method "Close" on type "*os.File" ``` Solution from: securego/gosec#512
…tements This is considered unsafe by gosec otherwise. See [1]. [1] securego/gosec#512
…tements in 'helper_http.go' This is considered unsafe by gosec otherwise. See [1]. This also makes sure to fix the warning with calling defer in a loop. [1] securego/gosec#512
…atements This is considered unsafe by gosec otherwise. [1] securego/gosec#512 [2] securego/gosec#714 [3] https://www.joeshaw.org/dont-defer-close-on-writable-files/
* Go: Bump github.com/securego/gosec/v2 from 2.14.0 to 2.15.0 Bumps [github.com/securego/gosec/v2](https://github.com/securego/gosec) from 2.14.0 to 2.15.0. - [Release notes](https://github.com/securego/gosec/releases) - [Changelog](https://github.com/securego/gosec/blob/master/.goreleaser.yml) - [Commits](securego/gosec@v2.14.0...v2.15.0) --- updated-dependencies: - dependency-name: github.com/securego/gosec/v2 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Make sure to handle error returned by io.Closer.Close() in 'defer' statements This is considered unsafe by gosec otherwise. [1] securego/gosec#512 [2] securego/gosec#714 [3] https://www.joeshaw.org/dont-defer-close-on-writable-files/ --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Armel Soro <asoro@redhat.com>
Summary
After update to latest Golang & gosec version, I see this security error:
My function never changes and looks like:
Steps to reproduce the behavior
gosec version
Go version (output of 'go version')
go version go1.15 linux/amd64
Operating system / Environment
Expected behavior
No errors, or solve this error.
Actual behavior
CI (GitHub Actions) send warnings and skip my code to master branch (but this code wasn't changed and works fine at lower version).
The text was updated successfully, but these errors were encountered: