Fix possible panics in slice type assertion

Before accessing the slices `.Value()` we should type check that it is
that actual type, otherwise we will end-up in a panic.

Signed-off-by: Sascha Grunert <sgrunert@suse.com>
This commit is contained in:
Sascha Grunert 2020-01-22 12:27:15 +01:00
parent f1a114a628
commit ca666ae167
No known key found for this signature in database
GPG Key ID: 8CE029DD1A866E52
4 changed files with 8 additions and 16 deletions

View File

@ -155,11 +155,9 @@ func (c *Context) Float64Slice(name string) []float64 {
func lookupFloat64Slice(name string, set *flag.FlagSet) []float64 {
f := set.Lookup(name)
if f != nil {
parsed, err := (f.Value.(*Float64Slice)).Value(), error(nil)
if err != nil {
return nil
if slice, ok := f.Value.(*Float64Slice); ok {
return slice.Value()
}
return parsed
}
return nil
}

View File

@ -151,11 +151,9 @@ func (c *Context) Int64Slice(name string) []int64 {
func lookupInt64Slice(name string, set *flag.FlagSet) []int64 {
f := set.Lookup(name)
if f != nil {
parsed, err := (f.Value.(*Int64Slice)).Value(), error(nil)
if err != nil {
return nil
if slice, ok := f.Value.(*Int64Slice); ok {
return slice.Value()
}
return parsed
}
return nil
}

View File

@ -165,11 +165,9 @@ func (c *Context) IntSlice(name string) []int {
func lookupIntSlice(name string, set *flag.FlagSet) []int {
f := set.Lookup(name)
if f != nil {
parsed, err := (f.Value.(*IntSlice)).Value(), error(nil)
if err != nil {
return nil
if slice, ok := f.Value.(*IntSlice); ok {
return slice.Value()
}
return parsed
}
return nil
}

View File

@ -149,11 +149,9 @@ func (c *Context) StringSlice(name string) []string {
func lookupStringSlice(name string, set *flag.FlagSet) []string {
f := set.Lookup(name)
if f != nil {
parsed, err := (f.Value.(*StringSlice)).Value(), error(nil)
if err != nil {
return nil
if slice, ok := f.Value.(*StringSlice); ok {
return slice.Value()
}
return parsed
}
return nil
}