Skip to content

Commit

Permalink
fix CWE-703
Browse files Browse the repository at this point in the history
explicitly close the file and handle errors.

Close is considered unsafe to defer, because it might fail
  • Loading branch information
yuvalk committed Oct 4, 2021
1 parent f7b418c commit 718ff99
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/profilecreator/profilecreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func getMustGatherFullPaths(mustGatherPath string, suffix string) (string, error

func getNode(mustGatherDirPath, nodeName string) (*v1.Node, error) {
var node v1.Node

nodePathSuffix := path.Join(ClusterScopedResources, CoreNodes, nodeName)
path, err := getMustGatherFullPaths(mustGatherDirPath, nodePathSuffix)
if err != nil {
Expand All @@ -116,12 +117,14 @@ func getNode(mustGatherDirPath, nodeName string) (*v1.Node, error) {
if err != nil {
return nil, fmt.Errorf("failed to open %q: %v", path, err)
}
defer src.Close()

dec := k8syaml.NewYAMLOrJSONDecoder(src, 1024)
if err := dec.Decode(&node); err != nil {
return nil, fmt.Errorf("failed to decode %q: %v", path, err)
}
if err := src.Close(); err != nil {
return nil, fmt.Errorf("error closing file %s: %s", src.Name(), err)
}
return &node, nil
}

Expand Down Expand Up @@ -200,11 +203,13 @@ func GetMCP(mustGatherDirPath, mcpName string) (*machineconfigv1.MachineConfigPo
if err != nil {
return nil, fmt.Errorf("failed to open %q: %v", mcpPath, err)
}
defer src.Close()
dec := k8syaml.NewYAMLOrJSONDecoder(src, 1024)
if err := dec.Decode(&mcp); err != nil {
return nil, fmt.Errorf("failed to decode %q: %v", mcpPath, err)
}
if err := src.Close(); err != nil {
return nil, fmt.Errorf("error closing file %s: %s", src.Name(), err)
}
return &mcp, nil
}

Expand Down

0 comments on commit 718ff99

Please sign in to comment.