Skip to content

Commit

Permalink
Make sure to handle error returned by ReadCloser.Close in 'defer' sta…
Browse files Browse the repository at this point in the history
…tements

This is considered unsafe by gosec otherwise. See [1].

[1] securego/gosec#512
  • Loading branch information
rm3l committed Mar 27, 2023
1 parent 4396054 commit deebf3d
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,13 @@ func addFileToIgnoreFile(gitIgnoreFile, filename string, fs filesystem.Filesyste
// DisplayLog displays logs to user stdout with some color formatting
// numberOfLastLines limits the number of lines from the output when we are not following it
// TODO(feloy) sync with devfile library?
func DisplayLog(followLog bool, rd io.ReadCloser, writer io.Writer, compName string, numberOfLastLines int) (err error) {
func DisplayLog(followLog bool, rd io.ReadCloser, writer io.Writer, compName string, numberOfLastLines int) error {

defer rd.Close()
defer func() {
if err := rd.Close(); err != nil {
klog.V(4).Infof("err occurred while closing resource: %v", err)
}
}()

// Copy to stdout (in yellow)
color.Set(color.FgYellow)
Expand All @@ -510,14 +514,14 @@ func DisplayLog(followLog bool, rd io.ReadCloser, writer io.Writer, compName str
os.Exit(1)
}()

if _, err = io.Copy(writer, rd); err != nil {
if _, err := io.Copy(writer, rd); err != nil {
return fmt.Errorf("error followLoging logs for %s: %w", compName, err)
}

} else if numberOfLastLines == -1 {
// Copy to buffer (we aren't going to be followLoging the logs..)
buf := new(bytes.Buffer)
_, err = io.Copy(buf, rd)
_, err := io.Copy(buf, rd)
if err != nil {
return fmt.Errorf("unable to copy followLog to buffer: %w", err)
}
Expand Down Expand Up @@ -554,7 +558,7 @@ func DisplayLog(followLog bool, rd io.ReadCloser, writer io.Writer, compName str
}
}
}
return
return nil

}

Expand Down

0 comments on commit deebf3d

Please sign in to comment.