fix: prevent infinite loop in Segment._split_cells with non-unit characters#4066
Closed
thakoreh wants to merge 1 commit intoTextualize:masterfrom
Closed
fix: prevent infinite loop in Segment._split_cells with non-unit characters#4066thakoreh wants to merge 1 commit intoTextualize:masterfrom
thakoreh wants to merge 1 commit intoTextualize:masterfrom
Conversation
…acters Segment._split_cells could enter an infinite loop when cutting at positions within multi-codepoint grapheme clusters (e.g. ZWJ sequences like family emojis). The while True loop had no exit condition for cases where out_by was not 0, -1, or +1 and the cell_size checks didn't match. Added bounds checking and a max iteration guard that falls back to splitting at the closest position instead of looping forever. Fixes Textualize#3299
Contributor
|
Your example doesn't hang without these changes. I suspect this was generated with AI - please see https://github.com/Textualize/rich/blob/master/AI_POLICY.md |
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.
Problem
Fixes #3299
Segment._split_cellscould enter an infinite loop when cutting at positions within multi-codepoint grapheme clusters (e.g. ZWJ sequences like👨👩👧). Thewhile Trueloop had no exit condition for cases where:out_bywas not exactly 0, -1, or +1cell_sizechecks didn't match the characters at the split positionThis happens because some multi-codepoint clusters have intermediate positions where
cell_sizereturns 0 (for combining characters and ZWJ), and the loop keeps adjustingposwithout ever hitting the exact boundary.Fix
pos < 0andpos > len(text))len(text) + 2) that falls back to splitting at the closest positionTesting