Skip to content

[BUG] IndexError may occur in the Text.divide(). The code is not safe. #2963

@happyapplehorse

Description

@happyapplehorse

Describe the bug

There is a function which counts the length of each line as below when i use "Textual" :

def line_structure(content: Text) -> int:
   lines = content.split()
   line_count = []
   for line in lines:
        line_count.append(line.cell_len)
   return line_count

The Text.split() method will call Text.divide() method, in that method, the IndexError may occur in certain situations. It says:" IndexError: list index out of range"

Here are the reasons why this error occurs:
The divide() method comes from the source file text.py in rich as below:
https://github.com/Textualize/rich/blob/68224905f5dc7a3c765e04aba0460b75a95f5004/rich/text.py
The divede() method starts at line #1051 ( to prevent from seeing different positions)

The code that caused the error located at the #1097 or #1113 lines.
For instance, 

while True:
                    line_start, line_end = line_ranges[end_line_no]     #1113 line
                    if span_end < line_start:
                        upper_bound = end_line_no - 1
                    elif span_end > line_end:
                        lower_bound = end_line_no + 1
                    else:
                        break
                    end_line_no = (lower_bound + upper_bound) // 2

When the variable "end_line_no" = line_count = len(line_ranges), it will raise a IndexError.
If span_end > line_end, variable end_line_no may continue to increase until it equals line_count. There is no corresponding restriction to limit the value of end_line_no within the correct range. I don’t know why the value of span_end is greater than end_line, but I can only say that in some cases, at least in my code, it does occur. [The error message provided clearly indicates that the span_end at the time was 104 while the text_length (which equals to end_line) was 100 , sometimes they are other values.]

WechatIMG2

WechatIMG4

I have temporarily fixed this problem by adding some codes like

if end_line_no >= line_count:
    end_line_no = line_count - 1
    break

after line #1113 and #1097, and this bug has not occurred again.

Platform

Click to expand

I use Termux in an Andorid pad.
Python version is 3.11.3.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions