From 3eacf18ea8d327b9669800f0ceb1754172629923 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Mon, 8 Jun 2020 23:06:16 +0900 Subject: [PATCH] Fix panic at decoding of inlining pointer type with DisallowUnknownField option --- decode.go | 11 +++++++---- decode_test.go | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/decode.go b/decode.go index b0990c8e..973d2e96 100644 --- a/decode.go +++ b/decode.go @@ -362,14 +362,17 @@ func errDuplicateKey(msg string, tk *token.Token) *duplicateKeyError { } func (d *Decoder) deleteStructKeys(structValue reflect.Value, unknownFields map[string]ast.Node) error { - strType := structValue.Type() - structFieldMap, err := structFieldMap(strType) + structType := structValue.Type() + if structType.Kind() == reflect.Ptr { + structType = structType.Elem() + } + structFieldMap, err := structFieldMap(structType) if err != nil { return errors.Wrapf(err, "failed to create struct field map") } - for j := 0; j < strType.NumField(); j++ { - field := structValue.Type().Field(j) + for j := 0; j < structType.NumField(); j++ { + field := structType.Field(j) if isIgnoredStructField(field) { continue } diff --git a/decode_test.go b/decode_test.go index 166f56c7..e5b76f6c 100644 --- a/decode_test.go +++ b/decode_test.go @@ -1426,8 +1426,8 @@ c: }) t.Run("inline", func(t *testing.T) { var v struct { - Child `yaml:",inline"` - A string `yaml:"a"` + *Child `yaml:",inline"` + A string `yaml:"a"` } yml := `--- a: a