-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Go: Add query to detect lack of error handling for os.File.Close
on writable handles
#11496
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
Conversation
QHelp previews: go/ql/src/InconsistentCode/UnhandledCloseWritableHandle.qhelpWritable file handle closed without error handlingData written to a file handle may not immediately be flushed to the underlying storage medium by the operating system. Often, the data may be cached in memory until the handle is closed, or until a later point after that. Only calling RecommendationAlways check whether ExampleIn this first example, two calls to package main
import (
"os"
)
func example() error {
f, err := os.OpenFile("file.txt", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return err
}
if _, err := f.WriteString("Hello"); err != nil {
f.Close()
return err
}
f.Close()
return nil
} In the second example, a call to package main
import (
"os"
)
func example() error {
f, err := os.OpenFile("file.txt", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteString("Hello"); err != nil {
return err
}
return nil
} To correct this issue, handle errors arising from calls to package main
import (
"os"
)
func example() (exampleError error) {
f, err := os.OpenFile("file.txt", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return err
}
defer func() {
// try to close the file; if an error occurs, set the error returned by `example`
// to that error, but only if `WriteString` didn't already set it to something first
if err := f.Close(); exampleError == nil && err != nil {
exampleError = err
}
}()
if _, err := f.WriteString("Hello"); err != nil {
exampleError = err
}
return
} References
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry it took me so long to review this. Lots of minor niggles, plus a few questions.
go/ql/test/query-tests/InconsistentCode/UnhandledCloseWritableHandle/tests.go
Outdated
Show resolved
Hide resolved
e969526
to
41c4cd5
Compare
All my review comments have been addressed. I haven't looked through it thoroughly, but from memory I think that means it can be merged. But the tests are failing because rebasing changed the format of the test output slightly (I remember merging the PR which did that). |
41c4cd5
to
25f9078
Compare
@owen-mc there was one unresolved review comment about adding another example which doesn't use |
Based on discussions following from #11410, this adds a new query which looks for calls to
os.File.Close
on writable file handles, where resulting errors are not handled.This is useful, because data written to a file may not be flushed to the underlying storage medium until
os.File.Close
is called (or possibly even later than that). Therefore, any errors resulting from issues with the storage medium may not occur until that point. Deferring calls toos.File.Close
or ignoring errors returned by it may lead to silent data loss.