From b7eaaebec095a539e37b2208e2a381e7c40da4e7 Mon Sep 17 00:00:00 2001 From: Armel Soro Date: Tue, 16 May 2023 10:33:31 +0200 Subject: [PATCH] Make sure to handle error returned by io.Closer.Close() in 'defer' statements This is considered unsafe by gosec otherwise. [1] https://github.com/securego/gosec/issues/512 [2] https://github.com/securego/gosec/issues/714 [3] https://www.joeshaw.org/dont-defer-close-on-writable-files/ --- pkg/util/util.go | 18 ++++++++++++------ tests/helper/helper_http.go | 6 +++++- tests/helper/helper_registry.go | 8 +++++++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/pkg/util/util.go b/pkg/util/util.go index 5feb4ca4ca3..5f9b079afa9 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -492,7 +492,12 @@ func addFileToIgnoreFile(gitIgnoreFile, filename string, fs filesystem.Filesyste // TODO(feloy) sync with devfile library? func DisplayLog(followLog bool, rd io.ReadCloser, writer io.Writer, compName string, numberOfLastLines int) (err error) { - defer rd.Close() + defer func() { + cErr := rd.Close() + if err == nil { + err = cErr + } + }() // Copy to stdout (in yellow) color.Set(color.FgYellow) @@ -529,14 +534,15 @@ func DisplayLog(followLog bool, rd io.ReadCloser, writer io.Writer, compName str } else { reader := bufio.NewReader(rd) var lines []string + var line string for { - line, err := reader.ReadString('\n') + line, err = reader.ReadString('\n') if err != nil { if err != io.EOF { return err - } else { - break } + err = nil + break } lines = append(lines, line) @@ -548,13 +554,13 @@ func DisplayLog(followLog bool, rd io.ReadCloser, writer io.Writer, compName str } for i := index; i < len(lines); i++ { - _, err := fmt.Fprintf(writer, lines[i]) + _, err = fmt.Fprintf(writer, lines[i]) if err != nil { return err } } } - return + return err } diff --git a/tests/helper/helper_http.go b/tests/helper/helper_http.go index b2dbc50f4aa..f68a6059219 100644 --- a/tests/helper/helper_http.go +++ b/tests/helper/helper_http.go @@ -34,7 +34,11 @@ func HttpWaitForWithStatus(url string, match string, maxRetry int, interval int, time.Sleep(time.Duration(interval) * time.Second) continue } - defer resp.Body.Close() + defer func() { + if cErr := resp.Body.Close(); cErr != nil { + fmt.Fprintf(GinkgoWriter, "[warn] error closing response body: %v\n", cErr) + } + }() if resp.StatusCode == expectedCode { body, _ = io.ReadAll(resp.Body) if strings.Contains(string(body), match) { diff --git a/tests/helper/helper_registry.go b/tests/helper/helper_registry.go index 8d51902bfcf..3f1f3348410 100644 --- a/tests/helper/helper_registry.go +++ b/tests/helper/helper_registry.go @@ -6,6 +6,8 @@ import ( "net/http" "net/url" + . "github.com/onsi/ginkgo/v2" + "github.com/redhat-developer/odo/pkg/api" ) @@ -28,7 +30,11 @@ func (o Registry) GetIndex() ([]api.DevfileStack, error) { if err != nil { return nil, err } - defer resp.Body.Close() + defer func() { + if cErr := resp.Body.Close(); cErr != nil { + fmt.Fprintf(GinkgoWriter, "[warn] error closing response body: %v\n", cErr) + } + }() var target []api.DevfileStack err = json.NewDecoder(resp.Body).Decode(&target)