Skip to content

Commit 199cc71

Browse files
authored
Merge pull request #394 from grokify/alert-autofix-1
Potential fix for code scanning alert no. 1: Writable file handle closed without error handling
2 parents 8d86ebc + 4d843c7 commit 199cc71

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

authutil/token_store.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,19 @@ func ReadTokenFile(fpath string) (*oauth2.Token, error) {
3030
}
3131

3232
// WriteTokenFile writes a token file to the the filepaths.
33-
func WriteTokenFile(fpath string, tok *oauth2.Token) error {
33+
func WriteTokenFile(fpath string, tok *oauth2.Token) (err error) {
3434
f, err := os.OpenFile(fpath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
3535
if err != nil {
3636
return errorsutil.Wrap(err, "Unable to write OAuth token")
3737
}
38-
defer f.Close()
39-
return json.NewEncoder(f).Encode(tok)
38+
defer func() {
39+
cerr := f.Close()
40+
if err == nil && cerr != nil {
41+
err = cerr
42+
}
43+
}()
44+
err = json.NewEncoder(f).Encode(tok)
45+
return err
4046
}
4147

4248
type TokenStoreFile struct {

0 commit comments

Comments
 (0)