提交 c50bdaab 编写于 作者: S Steven Powell

Check for args length to prevent out of bounds panic

上级 460342a5
......@@ -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") {
......
......@@ -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())
......
......@@ -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())
})
}
......@@ -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() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册