Check for args length to prevent out of bounds panic

pull/10606/head
Steven Powell 2021-02-24 16:45:24 -07:00
parent 460342a5f2
commit c50bdaabfe
4 changed files with 21 additions and 2 deletions

View File

@ -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. // 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() { func setLastStartFlags() {
if os.Args[1] != "start" { if len(os.Args) < 2 || os.Args[1] != "start" {
return return
} }
if pflag.CommandLine.Changed("log_file") || pflag.CommandLine.Changed("log_dir") { if pflag.CommandLine.Changed("log_file") || pflag.CommandLine.Changed("log_dir") {

View File

@ -52,7 +52,7 @@ func args() string {
// Log details about the executed command. // Log details about the executed command.
func Log(startTime time.Time) { func Log(startTime time.Time) {
if !shouldLog() { if len(os.Args) < 2 || !shouldLog() {
return return
} }
r := newRow(os.Args[1], args(), userName(), version.GetVersion(), startTime, time.Now()) r := newRow(os.Args[1], args(), userName(), version.GetVersion(), startTime, time.Now())

View File

@ -20,6 +20,7 @@ import (
"os" "os"
"os/user" "os/user"
"testing" "testing"
"time"
"github.com/spf13/viper" "github.com/spf13/viper"
"k8s.io/minikube/pkg/minikube/config" "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())
})
} }

View File

@ -17,10 +17,12 @@ limitations under the License.
package integration package integration
import ( import (
"context"
"flag" "flag"
"fmt" "fmt"
"math" "math"
"os" "os"
"os/exec"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -62,6 +64,13 @@ func TestMain(m *testing.M) {
os.Exit(code) 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. // setMaxParallelism caps the max parallelism. Go assumes 1 core per test, whereas minikube needs 2 cores per test.
func setMaxParallelism() { func setMaxParallelism() {