rich icon indicating copy to clipboard operation
rich copied to clipboard

[BUG] Out-of-range negative style start hangs Text slicing

Open agammann opened this issue 1 month ago • 1 comments

  • [x] I checked the Rich text documentation and searched open and closed issues for this behavior.
  • [x] I could not find it in the FAQ.

Describe the bug

A style start before the beginning of a Text value makes a later slice hang. This also happens with stylize_before.

from rich.text import Text

text = Text("abc")
text.stylize("red", -4)
print(text[1:])  # does not return

With -3 instead of -4, the slice returns bc. With -4, text.spans contains Span(-1, 3, "red") and the slice did not complete in a bounded 2-second subprocess. I also reproduced the hang with stylize_before("red", -4).

Expected behavior

Slicing should finish, either by clipping an out-of-range style span or by rejecting the invalid start.

Platform

Windows 11, CPython 3.14.8, Rich 15.0.0 from main commit 9d8f9a372cc5916fd4781fec207ced7ddac2f08f. No terminal rendering is needed to reproduce this.

agammann avatar Nov 01 '26 01:11 agammann

I reproduced this bug and prepared a minimal fix that clamps out-of-range negative style start indices to zero in both Text.stylize() and Text.stylize_before().

The original reproduction hangs, while all 9 focused regression tests pass with the patch.

The investigation and implementation were AI-assisted using ChatGPT (GPT-6). The reproduction and targeted tests were executed locally.

Per the project's AI contribution policy, would @willmcgugan approve this approach for a PR? I can submit the patch and regression tests after approval.

yxwang1215 avatar Nov 02 '26 03:11 yxwang1215