Report invalid UTF-8 byte in a comment as a parsing error#2983
Open
ksss wants to merge 1 commit into
Open
Conversation
PR ruby#2973 fixed the lexer hang on an invalid UTF-8 byte by advancing one byte, but a byte inside a comment was then silently swallowed (comments scan until newline or EOF), so a malformed comment parsed successfully. Map an invalid byte to a sentinel code point (U+FFFD) and exclude that sentinel from the comment rule's character class. The comment now stops at the invalid byte, which the catch-all rule turns into an ErrorToken, so the parser reports a normal ParsingError. Valid input is unaffected: valid multibyte characters map to the existing dummy code point and valid single bytes to their own value, so U+FFFD is never produced by valid input. src/lexer.c is regenerated with re2c 4.3. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #2973.
Background
In #2973 @soutaro noted:
#2973 stopped the lexer from hanging by advancing one byte past an invalid UTF-8 byte, but a byte that fell inside a comment was then silently swallowed — comments scan until newline/EOF, so
# \xC2parsed successfully.Approach
The lexer is code-point based (
YYCTYPE = unsigned int, fed byrbs_peek). The trick is to give an invalid byte a sentinel code point and exclude it from the comment rule's character class:src/lexstate.c: an invalid byte (char_width == 0) now maps toU+FFFDinstead of its raw value.src/lexer.re: the comment rule becomes"#" (. \ [\x00�])*, so the comment stops at the invalid byte. The catch-all rule (*) then turns it into anErrorToken, which the parser reports as a normalRBS::ParsingError.U+FFFDis safe as a sentinel because valid input never produces it as a code point: valid multibyte characters map to the existing dummy code point (12523) and valid single bytes map to their own value (0..255). Even a genuineU+FFFDin the input is a 3-byte sequence that decodes to the dummy code point, not to0xFFFD.src/lexer.cis regenerated with re2c 4.3 (matching CI).rake test: 958 tests, 0 failures.Question on scope
This PR only touches the comment rule, which is the case @soutaro mentioned. The same "scan until a delimiter" shape also appears in string literals (
dqstring/sqstring), annotations (%a{...}etc.), quoted identifiers (`...`), and inline comments (--). An invalid byte there is still swallowed today.Should I extend the same sentinel exclusion to those rules in this PR, or keep this one focused on comments and handle the rest separately? Happy to go either way.
This PR description was written by Claude Code.