Skip to content

Commit

Permalink
Fix panic when unmarshaling into a map twice (#854)
Browse files Browse the repository at this point in the history
Fixes #851
  • Loading branch information
pelletier authored Feb 28, 2023
1 parent 8a416da commit 643c251
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
8 changes: 1 addition & 7 deletions unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 643c251

Please sign in to comment.