From 17eef2c67971c70e0825d10bac00004b5638b682 Mon Sep 17 00:00:00 2001 From: James Elliott Date: Thu, 17 Mar 2022 16:53:07 +1100 Subject: [PATCH] fix(commands): explicitly close files (#3031) This fixes an issue that could potentially cause problems with open files due to a deferred file close in a for loop. --- internal/commands/storage_run.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/commands/storage_run.go b/internal/commands/storage_run.go index 88aa52496..1193d672c 100644 --- a/internal/commands/storage_run.go +++ b/internal/commands/storage_run.go @@ -341,15 +341,20 @@ func storageTOTPExportRunE(cmd *cobra.Command, args []string) (err error) { fmt.Println(c.URI()) case storageExportFormatPNG: file, _ := os.Create(filepath.Join(dir, fmt.Sprintf("%s.png", c.Username))) - defer file.Close() if img, err = c.Image(256, 256); err != nil { + _ = file.Close() + return err } if err = png.Encode(file, img); err != nil { + _ = file.Close() + return err } + + _ = file.Close() } }