Skip to content

Commit 7baa23f

Browse files
authored
Decode: error on array table mismatched type (#804)
Prevent the decoder from continuing if it encounters a type it cannot decode an array table into. Fixes #799
1 parent 2d8433b commit 7baa23f

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

unmarshaler.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,9 @@ func (d *decoder) handleArrayTableCollectionLast(key ast.Iterator, v reflect.Val
344344
elem := v.Index(idx)
345345
_, err := d.handleArrayTable(key, elem)
346346
return v, err
347+
default:
348+
return reflect.Value{}, fmt.Errorf("toml: cannot decode array table into a %s", v.Type())
347349
}
348-
349-
return d.handleArrayTable(key, v)
350350
}
351351

352352
// When parsing an array table expression, each part of the key needs to be

unmarshaler_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,28 @@ B = "data"`,
17351735
}
17361736
},
17371737
},
1738+
{
1739+
desc: "kv that points to a slice",
1740+
input: "a.b.c = 'foo'",
1741+
gen: func() test {
1742+
doc := map[string][]string{}
1743+
return test{
1744+
target: &doc,
1745+
err: true,
1746+
}
1747+
},
1748+
},
1749+
{
1750+
desc: "kv that points to a pointer to a slice",
1751+
input: "a.b.c = 'foo'",
1752+
gen: func() test {
1753+
doc := map[string]*[]string{}
1754+
return test{
1755+
target: &doc,
1756+
err: true,
1757+
}
1758+
},
1759+
},
17381760
}
17391761

17401762
for _, e := range examples {
@@ -2413,6 +2435,22 @@ Host = 'main.domain.com'
24132435
require.Equal(t, expected, string(b))
24142436
}
24152437

2438+
func TestIssue799(t *testing.T) {
2439+
const testTOML = `
2440+
# notice the double brackets
2441+
[[test]]
2442+
answer = 42
2443+
`
2444+
2445+
var s struct {
2446+
// should be []map[string]int
2447+
Test map[string]int `toml:"test"`
2448+
}
2449+
2450+
err := toml.Unmarshal([]byte(testTOML), &s)
2451+
require.Error(t, err)
2452+
}
2453+
24162454
func TestUnmarshalDecodeErrors(t *testing.T) {
24172455
examples := []struct {
24182456
desc string

0 commit comments

Comments
 (0)