From c50bdaabfef50b1eab994ef6fd64d28f9ff05d1f Mon Sep 17 00:00:00 2001 From: Steven Powell Date: Wed, 24 Feb 2021 16:45:24 -0700 Subject: [PATCH] Check for args length to prevent out of bounds panic --- cmd/minikube/main.go | 2 +- pkg/minikube/audit/audit.go | 2 +- pkg/minikube/audit/audit_test.go | 10 ++++++++++ test/integration/main_test.go | 9 +++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/minikube/main.go b/cmd/minikube/main.go index f24355a525..d4a79f76eb 100644 --- a/cmd/minikube/main.go +++ b/cmd/minikube/main.go @@ -158,7 +158,7 @@ func setFlags() { // setLastStartFlags sets the log_file flag to lastStart.txt if start command and user doesn't specify log_file or log_dir flags. func setLastStartFlags() { - if os.Args[1] != "start" { + if len(os.Args) < 2 || os.Args[1] != "start" { return } if pflag.CommandLine.Changed("log_file") || pflag.CommandLine.Changed("log_dir") { diff --git a/pkg/minikube/audit/audit.go b/pkg/minikube/audit/audit.go index 8fab2fc32f..7deb879725 100644 --- a/pkg/minikube/audit/audit.go +++ b/pkg/minikube/audit/audit.go @@ -52,7 +52,7 @@ func args() string { // Log details about the executed command. func Log(startTime time.Time) { - if !shouldLog() { + if len(os.Args) < 2 || !shouldLog() { return } r := newRow(os.Args[1], args(), userName(), version.GetVersion(), startTime, time.Now()) diff --git a/pkg/minikube/audit/audit_test.go b/pkg/minikube/audit/audit_test.go index 05ae9affa6..7c36ae6556 100644 --- a/pkg/minikube/audit/audit_test.go +++ b/pkg/minikube/audit/audit_test.go @@ -20,6 +20,7 @@ import ( "os" "os/user" "testing" + "time" "github.com/spf13/viper" "k8s.io/minikube/pkg/minikube/config" @@ -167,4 +168,13 @@ func TestAudit(t *testing.T) { } } }) + + // Check if logging with limited args causes a panic + t.Run("Log", func(t *testing.T) { + oldArgs := os.Args + defer func() { os.Args = oldArgs }() + os.Args = []string{"minikube"} + + Log(time.Now()) + }) } diff --git a/test/integration/main_test.go b/test/integration/main_test.go index 922be0a969..1a72688bb7 100644 --- a/test/integration/main_test.go +++ b/test/integration/main_test.go @@ -17,10 +17,12 @@ limitations under the License. package integration import ( + "context" "flag" "fmt" "math" "os" + "os/exec" "runtime" "strconv" "strings" @@ -62,6 +64,13 @@ func TestMain(m *testing.M) { os.Exit(code) } +func TestMainNoArgs(t *testing.T) { + rr, err := Run(t, exec.CommandContext(context.Background(), Target())) + if err != nil { + t.Fatalf("failed running minikube with no args %q: %v", rr.Command(), err) + } +} + // setMaxParallelism caps the max parallelism. Go assumes 1 core per test, whereas minikube needs 2 cores per test. func setMaxParallelism() {