Avoid decoding nil pointer in map walker (#17048)

This commit is contained in:
Kyle Havlovitz 2023-04-19 10:23:38 -07:00 committed by GitHub
parent d7c488762e
commit 26128548a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 1 deletions

3
.changelog/17048.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
Fix an bug where decoding some Config structs with unset pointer fields could fail with `reflect: call of reflect.Value.Type on zero Value`.
```

View File

@ -113,7 +113,7 @@ func (w *mapWalker) MapElem(m, k, v reflect.Value) error {
return nil
}
if inner := v.Elem(); inner.Type() == typMapIfaceIface {
if inner := v.Elem(); inner.IsValid() && inner.Type() == typMapIfaceIface {
// map[interface{}]interface{}, attempt to weakly decode into string keys
var target map[string]interface{}
if err := mapstructure.WeakDecode(v.Interface(), &target); err != nil {

View File

@ -41,6 +41,16 @@ func TestMapWalk(t *testing.T) {
},
unexpected: true,
},
// ensure we don't panic from trying to call reflect.Value.Type
// on a nil pointer
"nil pointer": {
input: map[string]interface{}{
"foo": nil,
},
expected: map[string]interface{}{
"foo": nil,
},
},
// ensure nested maps get processed correctly
"nested": {
input: map[string]interface{}{