diff --git a/staging/src/k8s.io/component-base/cli/flag/string_slice_flag.go b/staging/src/k8s.io/component-base/cli/flag/string_slice_flag.go index 37a1d778a39d8..ad0d07d758ece 100644 --- a/staging/src/k8s.io/component-base/cli/flag/string_slice_flag.go +++ b/staging/src/k8s.io/component-base/cli/flag/string_slice_flag.go @@ -18,6 +18,7 @@ package flag import ( goflag "flag" + "fmt" "strings" "github.com/spf13/pflag" @@ -45,9 +46,11 @@ func (s *StringSlice) String() string { } func (s *StringSlice) Set(val string) error { - if s.value == nil || !s.changed { - v := make([]string, 0) - s.value = &v + if s.value == nil { + return fmt.Errorf("no target (nil pointer to []string)") + } + if !s.changed { + *s.value = make([]string, 0) } *s.value = append(*s.value, val) s.changed = true diff --git a/staging/src/k8s.io/component-base/cli/flag/string_slice_flag_test.go b/staging/src/k8s.io/component-base/cli/flag/string_slice_flag_test.go index 745ff05232854..6e5ef240679f6 100644 --- a/staging/src/k8s.io/component-base/cli/flag/string_slice_flag_test.go +++ b/staging/src/k8s.io/component-base/cli/flag/string_slice_flag_test.go @@ -83,8 +83,8 @@ func TestStringSlice(t *testing.T) { } else if err != nil { t.Errorf("%d: expected nil error, got %v", i, err) } - if !reflect.DeepEqual(*v.value, test.expected) { - t.Errorf("%d: expected %+v, got %+v", i, test.expected, *v.value) + if !reflect.DeepEqual(s, test.expected) { + t.Errorf("%d: expected %+v, got %+v", i, test.expected, s) } if v.changed != test.changed { t.Errorf("%d: expected %t got %t", i, test.changed, v.changed)