Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing tests for invalid control characters in comments #618

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ func (p *parser) parseExpression(b []byte) (ast.Reference, []byte, error) {
}

if b[0] == '#' {
_, rest := scanComment(b)
_, rest, err := scanComment(b)
if err != nil {
return ref, rest, err
}

return ref, rest, nil
}
Expand All @@ -129,7 +132,10 @@ func (p *parser) parseExpression(b []byte) (ast.Reference, []byte, error) {
b = p.parseWhitespace(b)

if len(b) > 0 && b[0] == '#' {
_, rest := scanComment(b)
_, rest, err := scanComment(b)
if err != nil {
return ref, rest, err
}

return ref, rest, nil
}
Expand Down Expand Up @@ -478,7 +484,10 @@ func (p *parser) parseOptionalWhitespaceCommentNewline(b []byte) ([]byte, error)
b = p.parseWhitespace(b)

if len(b) > 0 && b[0] == '#' {
_, b = scanComment(b)
_, b, err = scanComment(b)
if err != nil {
return nil, err
}
}

if len(b) == 0 {
Expand Down
18 changes: 15 additions & 3 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,31 @@ func scanWhitespace(b []byte) ([]byte, []byte) {
}

//nolint:unparam
func scanComment(b []byte) ([]byte, []byte) {
func scanComment(b []byte) ([]byte, []byte, error) {
// comment-start-symbol = %x23 ; #
// non-ascii = %x80-D7FF / %xE000-10FFFF
// non-eol = %x09 / %x20-7F / non-ascii
//
// comment = comment-start-symbol *non-eol
for i := 1; i < len(b); i++ {
if b[i] == 0x7f {
return b[:i], b[i:], newDecodeError(b[:i], "comments cannot include the 0x7f DEL character")
}
if b[i] == 0x0a {
return b[:i], b[i:], newDecodeError(b[:i], "comments cannot include the 0x0a LF character")
}
if b[i] == 0x00 {
return b[:i], b[i:], newDecodeError(b[:i], "comments cannot include the 0x00 NUL character")
}
if b[i] == 0x1f {
return b[:i], b[i:], newDecodeError(b[:i], "comments cannot include the 0x1f US character")
}
if b[i] == '\n' {
return b[:i], b[i:]
return b[:i], b[i:], nil
}
}

return b, b[len(b):]
return b, b[len(b):], nil
}

func scanBasicString(b []byte) ([]byte, []byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions toml_testgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ func TestTOMLTest_Invalid_Control_CommentLf(t *testing.T) {
}

func TestTOMLTest_Invalid_Control_CommentNull(t *testing.T) {
input := "comment-null = \"null\" # \x00\n"
input := "comment-null = \"null\" # \u0000\n"
testgenInvalid(t, input)
}

func TestTOMLTest_Invalid_Control_CommentUs(t *testing.T) {
input := "comment-us = \"ctrl-_\" # \x1f\n"
input := "comment-us = \"ctrl-_\" # \u001f\n"
testgenInvalid(t, input)
}

Expand Down