From 40cfb6f45824f8f5cd16957894b5c42e10b65f6e Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Mon, 6 Sep 2021 12:18:45 -0400 Subject: [PATCH] parser: don't crash on unterminated table key (#580) * parser: don't crash on unterminated table key Fixes #579 * parser: fix format of error returned by expect EOF was missing the format string and %U is not very human friendly. --- parser.go | 6 +++++- unmarshaler_test.go | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/parser.go b/parser.go index 03385c1..1781cc9 100644 --- a/parser.go +++ b/parser.go @@ -1030,8 +1030,12 @@ func isValidBinaryRune(r byte) bool { } func expect(x byte, b []byte) ([]byte, error) { + if len(b) == 0 { + return nil, newDecodeError(b, "expected character %c but the document ended here", x) + } + if b[0] != x { - return nil, newDecodeError(b[0:1], "expected character %U", x) + return nil, newDecodeError(b[0:1], "expected character %c", x) } return b[1:], nil diff --git a/unmarshaler_test.go b/unmarshaler_test.go index 89b96fd..008dfda 100644 --- a/unmarshaler_test.go +++ b/unmarshaler_test.go @@ -1722,6 +1722,12 @@ func TestIssue507(t *testing.T) { require.Error(t, err) } +func TestIssue579(t *testing.T) { + var v interface{} + err := toml.Unmarshal([]byte(`[foo`), &v) + require.Error(t, err) +} + //nolint:funlen func TestUnmarshalDecodeErrors(t *testing.T) { examples := []struct {