Skip to content

Commit 643c251

Browse files
authored
Fix panic when unmarshaling into a map twice (#854)
Fixes #851
1 parent 8a416da commit 643c251

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

unmarshaler.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -1039,15 +1039,9 @@ func (d *decoder) handleKeyValuePart(key unstable.Iterator, value *unstable.Node
10391039

10401040
mv := v.MapIndex(mk)
10411041
set := false
1042-
if !mv.IsValid() {
1042+
if !mv.IsValid() || key.IsLast() {
10431043
set = true
10441044
mv = reflect.New(v.Type().Elem()).Elem()
1045-
} else {
1046-
if key.IsLast() {
1047-
var x interface{}
1048-
mv = reflect.ValueOf(&x).Elem()
1049-
set = true
1050-
}
10511045
}
10521046

10531047
nv, err := d.handleKeyValueInner(key, value, mv)

unmarshaler_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -2472,6 +2472,21 @@ func TestIssue850(t *testing.T) {
24722472
require.Error(t, err)
24732473
}
24742474

2475+
func TestIssue851(t *testing.T) {
2476+
type Target struct {
2477+
Params map[string]string `toml:"params"`
2478+
}
2479+
2480+
content := "params = {a=\"1\",b=\"2\"}"
2481+
var target Target
2482+
err := toml.Unmarshal([]byte(content), &target)
2483+
require.NoError(t, err)
2484+
require.Equal(t, map[string]string{"a": "1", "b": "2"}, target.Params)
2485+
err = toml.Unmarshal([]byte(content), &target)
2486+
require.NoError(t, err)
2487+
require.Equal(t, map[string]string{"a": "1", "b": "2"}, target.Params)
2488+
}
2489+
24752490
func TestUnmarshalDecodeErrors(t *testing.T) {
24762491
examples := []struct {
24772492
desc string

0 commit comments

Comments
 (0)