[GH-ISSUE #939] Out-of-bounds panic when text wrapping is enabled #687

Closed
opened 2026-03-04 01:07:02 +03:00 by kerem · 3 comments
Owner

Originally created by @darkhz on GitHub (Jan 17, 2024).
Original GitHub issue: https://github.com/rivo/tview/issues/939

When text-wrapping is enabled on an empty textview, i.e. set by (*tview.TextView).SetWrap(true) and ScrollToHighlight() is used,
an out of bounds panic occurs when a region is highlighted, but the lineIndex buffer is empty (textview.go#L1070C21-L1070C21).

Maybe check whether the lineIndex buffer is empty before getting highlights?

if t.wrap && len(lineIndex) > fromHighlight { 				
}
Originally created by @darkhz on GitHub (Jan 17, 2024). Original GitHub issue: https://github.com/rivo/tview/issues/939 When **text-wrapping** is enabled on an empty textview, i.e. set by `(*tview.TextView).SetWrap(true)` and `ScrollToHighlight()` is used, an out of bounds panic occurs when a region is highlighted, but the `lineIndex` buffer is empty [(textview.go#L1070C21-L1070C21)](https://github.com/rivo/tview/blob/bf8f1c43e46c5ecbeda080d2e24273545b323073/textview.go#L1070C21-L1070C21). Maybe check whether the `lineIndex` buffer is empty before getting highlights? ```go if t.wrap && len(lineIndex) > fromHighlight { } ```
kerem closed this issue 2026-03-04 01:07:02 +03:00
Author
Owner

@rivo commented on GitHub (Jan 17, 2024):

Can you please provide a small program that reproduces this error?

<!-- gh-comment-id:1895013272 --> @rivo commented on GitHub (Jan 17, 2024): Can you please provide a small program that reproduces this error?
Author
Owner

@darkhz commented on GitHub (Jan 17, 2024):

Here is a small example to illustrate the above issue.
I have also edited the first comment above for clarification.

func main() {
	tv := tview.NewTextView()
	tv.SetWrap(true)
	tv.SetRegions(true)
	tv.SetDynamicColors(true)
	tv.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Rune() == 'h' {
			tv.Highlight("region")
			tv.ScrollToHighlight()
		}

		return event
	})

        tview.NewApplication().SetRoot(tv, true).Run()
}
<!-- gh-comment-id:1895148134 --> @darkhz commented on GitHub (Jan 17, 2024): Here is a small example to illustrate the above issue. I have also edited the first comment above for clarification. ```go func main() { tv := tview.NewTextView() tv.SetWrap(true) tv.SetRegions(true) tv.SetDynamicColors(true) tv.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if event.Rune() == 'h' { tv.Highlight("region") tv.ScrollToHighlight() } return event }) tview.NewApplication().SetRoot(tv, true).Run() } ```
Author
Owner

@rivo commented on GitHub (Jan 18, 2024):

Thank you. The latest commit should fix this. There were two issues here:

  • The Highlight method could highlight regions that didn't exist in the text. That is now not possible anymore.
  • One could cause the line index to be reset between a call to Highlight and the next time the text view is drawn. In that case, we have to parse again until all highlighted regions are known. So I added that, too.
<!-- gh-comment-id:1898125085 --> @rivo commented on GitHub (Jan 18, 2024): Thank you. The latest commit should fix this. There were two issues here: - The `Highlight` method could highlight regions that didn't exist in the text. That is now not possible anymore. - One could cause the line index to be reset between a call to `Highlight` and the next time the text view is drawn. In that case, we have to parse again until all highlighted regions are known. So I added that, too.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#687
No description provided.