Reject reversed position ranges in parser/lexer entrypoints#2974
Open
ksss wants to merge 1 commit into
Open
Conversation
`alloc_parser_from_buffer` and `alloc_lexer_from_buffer` in the C
extension already rejected negative positions, but accepted ranges
where `start_pos > end_pos`. The lexer's main loop condition is
`current.byte_pos == end_pos`, so a reversed range never terminates:
`current` advances past `end_pos` and the equality is never satisfied,
producing an infinite loop with the GVL held (SIGINT ineffective).
Add the new guard alongside the existing negative-position check and
extract both into a `validate_position_range` helper shared by both
entrypoints.
Minimal reproducer (public API, used to hang indefinitely):
RBS::Parser.parse_type("", byte_range: 1..0)
Found by fuzzing the parser entry points with randomized position
arguments.
Co-Authored-By: Claude Opus 4.7 (1M context) <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.
Summary
alloc_parser_from_bufferandalloc_lexer_from_bufferalready rejected negative positions but accepted ranges wherestart_pos > end_pos. Such a range made the lexer's main loop condition (current.byte_pos == end_pos) unsatisfiable:currentadvances pastend_posand the equality is never reached, producing an infinite loop in the C extension with the GVL held (SIGINTineffective).Reproducer
The same hang is reachable from
RBS::Parser.parse_method_typevia itsbyte_range:keyword, and from the low-level_parse_signature/_parse_type/_parse_method_typedirectly with reversedstart_pos/end_posarguments. The_lexentrypoint always passesstart_pos = 0, so the new guard on the lexer side is defensive rather than a present-day bug fix.Fix
Add a reversed-range guard alongside the existing negative-position check, and extract both into a
validate_position_rangehelper shared byalloc_lexer_from_bufferandalloc_parser_from_buffer:Reversed ranges now raise
ArgumentErrorimmediately instead of reaching the lexer.This PR description was written by Claude Code.