From 2638d7e01aba5232b5bf119df6fd0756582569fa Mon Sep 17 00:00:00 2001 From: Spencer McCreary Date: Tue, 26 Mar 2024 11:52:21 -0400 Subject: [PATCH 1/4] fix: close file on panic. If write were to panic, the file would not get closed. Adding a defer and checking if 'f' is nil prior to closing ensures we will always close the file and only once. --- util/util.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/util/util.go b/util/util.go index 5c77128..a6e51e6 100644 --- a/util/util.go +++ b/util/util.go @@ -101,13 +101,20 @@ func WriteFile(fs billy.Basic, filename string, data []byte, perm os.FileMode) e if err != nil { return err } + defer func() { + if f != nil { + _ = f.Close() + } + }() n, err := f.Write(data) if err == nil && n < len(data) { err = io.ErrShortWrite } - if err1 := f.Close(); err == nil { + err1 := f.Close() + f = nil + if err == nil { err = err1 } From 12ecebef0d7faaf4f45e0bdde2311b1269a47c04 Mon Sep 17 00:00:00 2001 From: Spencer McCreary Date: Tue, 26 Mar 2024 11:56:15 -0400 Subject: [PATCH 2/4] fix: explictly ignore close err --- util/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/util.go b/util/util.go index a6e51e6..e08843a 100644 --- a/util/util.go +++ b/util/util.go @@ -248,7 +248,7 @@ func ReadFile(fs billy.Basic, name string) ([]byte, error) { return nil, err } - defer f.Close() + defer func() { _ = f.Close() }() var size int if info, err := fs.Stat(name); err == nil { From c98c36d54223429d2c1069669312f14b6d13f63c Mon Sep 17 00:00:00 2001 From: Spencer McCreary Date: Tue, 26 Mar 2024 15:21:18 -0400 Subject: [PATCH 3/4] code-review --- util/util.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/util/util.go b/util/util.go index e08843a..a477932 100644 --- a/util/util.go +++ b/util/util.go @@ -96,14 +96,14 @@ func removeAll(fs billy.Basic, path string) error { // WriteFile writes data to a file named by filename in the given filesystem. // If the file does not exist, WriteFile creates it with permissions perm; // otherwise WriteFile truncates it before writing. -func WriteFile(fs billy.Basic, filename string, data []byte, perm os.FileMode) error { +func WriteFile(fs billy.Basic, filename string, data []byte, perm os.FileMode) (err error) { f, err := fs.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) if err != nil { return err } defer func() { if f != nil { - _ = f.Close() + err = f.Close() } }() @@ -112,13 +112,7 @@ func WriteFile(fs billy.Basic, filename string, data []byte, perm os.FileMode) e err = io.ErrShortWrite } - err1 := f.Close() - f = nil - if err == nil { - err = err1 - } - - return err + return nil } // Random number state. @@ -248,7 +242,7 @@ func ReadFile(fs billy.Basic, name string) ([]byte, error) { return nil, err } - defer func() { _ = f.Close() }() + defer f.Close() var size int if info, err := fs.Stat(name); err == nil { From ab39b3ef658ef5f43cfa9f498a0c82b3512dcebc Mon Sep 17 00:00:00 2001 From: Spencer McCreary Date: Tue, 26 Mar 2024 15:33:51 -0400 Subject: [PATCH 4/4] fix: dont overwrite possible io.ErrShortWrite --- util/util.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/util/util.go b/util/util.go index a477932..9fae2ae 100644 --- a/util/util.go +++ b/util/util.go @@ -103,7 +103,10 @@ func WriteFile(fs billy.Basic, filename string, data []byte, perm os.FileMode) ( } defer func() { if f != nil { - err = f.Close() + err1 := f.Close() + if err == nil { + err = err1 + } } }()