From 7662889c4381b545dc9d1b29b0d108c3d56f9c91 Mon Sep 17 00:00:00 2001 From: Brian Downs <brian.downs@gmail.com> Date: Mon, 23 Aug 2021 15:16:26 -0700 Subject: [PATCH] update type assertion to prevent panic (#50) Signed-off-by: Brian Downs <brian.downs@gmail.com> --- pkg/config/config.go | 2 -- pkg/config/config_unix.go | 7 ++++++- pkg/image/util.go | 1 - pkg/prober/prober.go | 1 - 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index e8d2c0f9..169c75fc 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -50,11 +50,9 @@ func Parse(path string, result interface{}) error { } f, err := os.Open(path) - if err != nil { return err } - defer f.Close() file := filepath.Base(path) diff --git a/pkg/config/config_unix.go b/pkg/config/config_unix.go index f5a23ed0..3e7b288f 100644 --- a/pkg/config/config_unix.go +++ b/pkg/config/config_unix.go @@ -3,13 +3,18 @@ package config import ( + "errors" "fmt" "os" "syscall" ) func pathOwnedByRoot(fi os.FileInfo, path string) error { - if fi.Sys().(*syscall.Stat_t).Uid != 0 || fi.Sys().(*syscall.Stat_t).Gid != 0 { + stat, ok := fi.Sys().(*syscall.Stat_t) + if !ok { + return errors.New("failed type assertion for *syscall.Stat_t") + } + if stat.Uid != 0 || stat.Gid != 0 { return fmt.Errorf("file %s had was not owned by root:root", path) } return nil diff --git a/pkg/image/util.go b/pkg/image/util.go index 4cb29de1..9abdbbfa 100644 --- a/pkg/image/util.go +++ b/pkg/image/util.go @@ -67,7 +67,6 @@ func NewUtility(imagesDir, imageCredentialProviderConfig, imageCredentialProvide } func (u *Utility) Stage(destDir string, imgString string) error { - if err := os.MkdirAll(destDir, 0755); err != nil { return err } diff --git a/pkg/prober/prober.go b/pkg/prober/prober.go index c68c6c69..94aa2d93 100644 --- a/pkg/prober/prober.go +++ b/pkg/prober/prober.go @@ -59,7 +59,6 @@ func DoProbe(probe Probe, probeStatus *ProbeStatus, initial bool) error { } caCertPool, err := x509.SystemCertPool() - if err != nil { caCertPool = x509.NewCertPool() logrus.Errorf("error loading system cert pool: %v", err)