From a0285163e1cf3c4d02a817aa1ed1d4cffc7e0a37 Mon Sep 17 00:00:00 2001 From: Dan Rollo Date: Tue, 11 Aug 2020 19:26:27 -0400 Subject: [PATCH] ensure BindPFlag() detects a nil flag parameter before wrapping in pflagValue. --- viper.go | 3 +++ viper_test.go | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/viper.go b/viper.go index c3130c2..8889f87 100644 --- a/viper.go +++ b/viper.go @@ -992,6 +992,9 @@ func (v *Viper) BindPFlags(flags *pflag.FlagSet) error { func BindPFlag(key string, flag *pflag.Flag) error { return v.BindPFlag(key, flag) } func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error { + if flag == nil { + return fmt.Errorf("flag for %q is nil", key) + } return v.BindFlagValue(key, pflagValue{flag}) } diff --git a/viper_test.go b/viper_test.go index 09d5021..c38c665 100644 --- a/viper_test.go +++ b/viper_test.go @@ -970,6 +970,11 @@ func TestBindPFlag(t *testing.T) { assert.Equal(t, "testing_mutate", Get("testvalue")) } +func TestBindPFlagDetectNilFlag(t *testing.T) { + result := BindPFlag("testvalue", nil) + assert.Error(t, result) +} + func TestBindPFlagStringToString(t *testing.T) { tests := []struct { Expected map[string]string