Fix lexer infinite loop / abort on invalid UTF-8 byte#2973
Open
ksss wants to merge 1 commit into
Open
Conversation
When the active encoding's `char_width` returned 0 for a byte,
`rbs_next_char` left `byte_len = 0`. The lexer then either looped
forever (when the byte was inside a comment) or tripped
`RBS_ASSERT(current_character_bytes > 0, ...)` in `rbs_skip` at top
level.
Treat such a byte as a 1-byte garbage character so the lexer always
advances at least one byte. The invalid byte then surfaces as a
regular parsing error through the existing error path.
Minimal reproducer that used to hang the host process indefinitely
with the GVL held:
RBS::Parser._parse_signature(
RBS::Buffer.new(content: "# \xC2".force_encoding("UTF-8"),
name: "x.rbs"),
0, 3
)
Found by fuzzing the parser entry points with random byte mutations
of the existing seed RBS files.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
4d292ab to
e9612b4
Compare
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.
Summary
rbs_next_charleftbyte_len = 0when the active encoding'schar_width()rejected a byte. Depending on where the byte appeared, the lexer either:SIGINTineffective), orRBS_ASSERT(current_character_bytes > 0, ...)inrbs_skipat top level and calledexit(1).This PR makes
rbs_next_charadvance one byte in that case so the lexer always makes progress; the invalid byte then flows into the usual parser error path.Reproducer
Fix
src/lexstate.c(rbs_next_char): whenencoding->char_width()returns0, treat the byte as a 1-byte garbage character and advance one byte. The lexer's invariant "cursor advances by at least one byte per step" is now preserved on every code path. Valid UTF-8 input is unaffected becausechar_width()never returns0for valid sequences.This PR description was written by Claude Code.