Skip to content

Commit

Permalink
Merge pull request #251 from eh-steve/bugfix-panic-with-empty-compose…
Browse files Browse the repository at this point in the history
…d-decodehooks

Fix possible panic when using ComposeDecodeHookFunc() with no funcs
  • Loading branch information
mitchellh committed Sep 14, 2021
2 parents 4664f9e + edc5649 commit 6577afa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions decode_hooks.go
Expand Up @@ -63,6 +63,11 @@ func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc {
return func(f reflect.Value, t reflect.Value) (interface{}, error) {
var err error
var data interface{}

if len(fs) == 0 {
data = f.Interface()
}

newFrom := f
for _, f1 := range fs {
data, err = DecodeHookExec(f1, newFrom, t)
Expand Down
32 changes: 32 additions & 0 deletions decode_hooks_test.go
Expand Up @@ -84,6 +84,38 @@ func TestComposeDecodeHookFunc_kinds(t *testing.T) {
}
}

func TestComposeDecodeHookFunc_safe_nofuncs(t *testing.T) {
f := ComposeDecodeHookFunc()
type myStruct2 struct {
MyInt int
}

type myStruct1 struct {
Blah map[string]myStruct2
}

src := &myStruct1{Blah: map[string]myStruct2{
"test": {
MyInt: 1,
},
}}

dst := &myStruct1{}
dConf := &DecoderConfig{
Result: dst,
ErrorUnused: true,
DecodeHook: f,
}
d, err := NewDecoder(dConf)
if err != nil {
t.Fatal(err)
}
err = d.Decode(src)
if err != nil {
t.Fatal(err)
}
}

func TestStringToSliceHookFunc(t *testing.T) {
f := StringToSliceHookFunc(",")

Expand Down

0 comments on commit 6577afa

Please sign in to comment.