Skip to content

Commit

Permalink
parser: don't crash on unterminated table key (#580)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
pelletier authored Sep 6, 2021
1 parent 1230ca4 commit 40cfb6f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 40cfb6f

Please sign in to comment.