From 79e78b234ce943a680638cf3cf7dcf07cbf36447 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Wed, 24 Nov 2021 18:50:04 -0500 Subject: [PATCH] Decoder: fix panic on table array behind a pointer (#682) Fixes #677 --- unmarshaler.go | 4 +++- unmarshaler_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/unmarshaler.go b/unmarshaler.go index 722e47d..46b4460 100644 --- a/unmarshaler.go +++ b/unmarshaler.go @@ -345,7 +345,9 @@ func (d *decoder) handleArrayTableCollection(key ast.Iterator, v reflect.Value) if err != nil { return reflect.Value{}, err } - v.Elem().Set(elem) + if elem.IsValid() { + v.Elem().Set(elem) + } return v, nil case reflect.Slice: diff --git a/unmarshaler_test.go b/unmarshaler_test.go index b2bc7be..8823f8c 100644 --- a/unmarshaler_test.go +++ b/unmarshaler_test.go @@ -2220,6 +2220,42 @@ func TestIssue666(t *testing.T) { require.Error(t, err) } +func TestIssue677(t *testing.T) { + doc := ` +[Build] +Name = "publication build" + +[[Build.Dependencies]] +Name = "command" +Program = "hugo" +` + + type _tomlJob struct { + Dependencies []map[string]interface{} + } + + type tomlParser struct { + Build *_tomlJob + } + + p := tomlParser{} + + err := toml.Unmarshal([]byte(doc), &p) + require.NoError(t, err) + + expected := tomlParser{ + Build: &_tomlJob{ + Dependencies: []map[string]interface{}{ + { + "Name": "command", + "Program": "hugo", + }, + }, + }, + } + require.Equal(t, expected, p) +} + //nolint:funlen func TestUnmarshalDecodeErrors(t *testing.T) { examples := []struct {