cli / go-gh

A Go module for interacting with gh and the GitHub API from the command line.
https://pkg.go.dev/github.com/cli/go-gh/v2
MIT License
322 stars 45 forks source link

Properly handle closing of files after writing #113

Closed samcoe closed 1 year ago

samcoe commented 1 year ago

Addresses two CodeQL warnings about properly handling the return value when closing a file that has been written to.

Fixes https://github.com/cli/go-gh/security/code-scanning/13 Fixes https://github.com/cli/go-gh/security/code-scanning/14

samcoe commented 1 year ago

@mislav I think the CodeQL warnings are not exactly around having an unhandled error and more that since we are writing to these files the data might be cached until the file handle is closed, so the errors that happen here are most likely to be errors writing that cached data to the file which feel more important than just a standard file closing error. We do normal expose write errors to the user as they indicate a potential lose of data.

Note there are other use cases of defer f.Close() that I didn't fix up because the file is only being read from and not written to.