Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
happyapplehorse opened this issue May 14, 2023 · 4 comments
Closed
2 tasks done

Comments

@happyapplehorse
Copy link

happyapplehorse commented May 14, 2023

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.

@github-actions
Copy link

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@willmcgugan
Copy link
Collaborator

Can you give me a working example to reproduce this issue.

@willmcgugan
Copy link
Collaborator

Please reopen if you can give me an example I can use to reproduce this.

@github-actions
Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants