From fa31be9144bd274d047435010a1fe5ccd09c262f Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Tue, 28 Feb 2023 16:47:27 +0100 Subject: [PATCH] Fix error report of type mismatch on inline tables Parser did not track the location of the faulty inline table in the document, and unmarshaler tried to the use the non-raw data field of the AST node, both resulting into a panic when generating the parser error. Fixes #850 --- unmarshaler.go | 2 +- unmarshaler_test.go | 6 ++++++ unstable/parser.go | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/unmarshaler.go b/unmarshaler.go index 5f684075..7383afe9 100644 --- a/unmarshaler.go +++ b/unmarshaler.go @@ -746,7 +746,7 @@ func (d *decoder) unmarshalInlineTable(itable *unstable.Node, v reflect.Value) e } return d.unmarshalInlineTable(itable, elem) default: - return unstable.NewParserError(itable.Data, "cannot store inline table in Go type %s", v.Kind()) + return unstable.NewParserError(d.p.Raw(itable.Raw), "cannot store inline table in Go type %s", v.Kind()) } it := itable.Children() diff --git a/unmarshaler_test.go b/unmarshaler_test.go index a074a8af..f7ccc598 100644 --- a/unmarshaler_test.go +++ b/unmarshaler_test.go @@ -2466,6 +2466,12 @@ func TestIssue807(t *testing.T) { require.Equal(t, "foo", m.Name) } +func TestIssue850(t *testing.T) { + data := make(map[string]string) + err := toml.Unmarshal([]byte("foo = {}"), &data) + require.Error(t, err) +} + func TestUnmarshalDecodeErrors(t *testing.T) { examples := []struct { desc string diff --git a/unstable/parser.go b/unstable/parser.go index 14508466..571f4e95 100644 --- a/unstable/parser.go +++ b/unstable/parser.go @@ -402,6 +402,7 @@ func (p *Parser) parseInlineTable(b []byte) (reference, []byte, error) { // inline-table-keyvals = keyval [ inline-table-sep inline-table-keyvals ] parent := p.builder.Push(Node{ Kind: InlineTable, + Raw: p.Range(b[:1]), }) first := true