From 643c251c4b34637fef522646c3513598cd4b90f7 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Tue, 28 Feb 2023 17:34:24 +0100 Subject: [PATCH] Fix panic when unmarshaling into a map twice (#854) Fixes #851 --- unmarshaler.go | 8 +------- unmarshaler_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/unmarshaler.go b/unmarshaler.go index 7383afe9..5c19845e 100644 --- a/unmarshaler.go +++ b/unmarshaler.go @@ -1039,15 +1039,9 @@ func (d *decoder) handleKeyValuePart(key unstable.Iterator, value *unstable.Node mv := v.MapIndex(mk) set := false - if !mv.IsValid() { + if !mv.IsValid() || key.IsLast() { set = true mv = reflect.New(v.Type().Elem()).Elem() - } else { - if key.IsLast() { - var x interface{} - mv = reflect.ValueOf(&x).Elem() - set = true - } } nv, err := d.handleKeyValueInner(key, value, mv) diff --git a/unmarshaler_test.go b/unmarshaler_test.go index f7ccc598..3c344250 100644 --- a/unmarshaler_test.go +++ b/unmarshaler_test.go @@ -2472,6 +2472,21 @@ func TestIssue850(t *testing.T) { require.Error(t, err) } +func TestIssue851(t *testing.T) { + type Target struct { + Params map[string]string `toml:"params"` + } + + content := "params = {a=\"1\",b=\"2\"}" + var target Target + err := toml.Unmarshal([]byte(content), &target) + require.NoError(t, err) + require.Equal(t, map[string]string{"a": "1", "b": "2"}, target.Params) + err = toml.Unmarshal([]byte(content), &target) + require.NoError(t, err) + require.Equal(t, map[string]string{"a": "1", "b": "2"}, target.Params) +} + func TestUnmarshalDecodeErrors(t *testing.T) { examples := []struct { desc string