[GH-ISSUE #429] Panic on TextView.reindexBuffer #314

Closed
opened 2026-03-04 01:03:52 +03:00 by kerem · 5 comments
Owner

Originally created by @pengux on GitHub (Apr 11, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/429

Not sure what's the problem is but it should probably do a bound check and return an error instead of panic if it is out of bound. The stack trace:

panic: runtime error: slice bounds out of range [321:252] [recovered]
        panic: runtime error: slice bounds out of range [321:252]

goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1(0xc000885880)
        /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/application.go:244 +0x82
panic(0x8bde60, 0xc00039a160)
        /usr/lib/go/src/runtime/panic.go:967 +0x166
github.com/rivo/tview.(*TextView).reindexBuffer(0xc00143a000, 0x8c)
        /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/textview.go:719 +0x15bb
github.com/rivo/tview.(*TextView).Draw(0xc00143a000, 0x99a5a0, 0xc000cbe1c0)
        /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/textview.go:861 +0x17b
github.com/rivo/tview.(*Flex).Draw(0xc001804330, 0x99a5a0, 0xc000cbe1c0)
        /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/flex.go:183 +0x344
github.com/rivo/tview.(*Flex).Draw(0xc0018042d0, 0x99a5a0, 0xc000cbe1c0)
        /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/flex.go:183 +0x344
github.com/rivo/tview.(*Pages).Draw(0xc0018042a0, 0x99a5a0, 0xc000cbe1c0)
        /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/pages.go:278 +0xa8
github.com/rivo/tview.(*Application).draw(0xc000885880, 0x0)
        /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/application.go:582 +0xda
github.com/rivo/tview.(*Application).Run(0xc000885880, 0x0, 0x0)
        /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/application.go:332 +0x77d
main.run(0xc000036a80, 0xc00000f300, 0x1)
 
Originally created by @pengux on GitHub (Apr 11, 2020). Original GitHub issue: https://github.com/rivo/tview/issues/429 Not sure what's the problem is but it should probably do a bound check and return an error instead of panic if it is out of bound. The stack trace: ``` panic: runtime error: slice bounds out of range [321:252] [recovered] panic: runtime error: slice bounds out of range [321:252] goroutine 1 [running]: github.com/rivo/tview.(*Application).Run.func1(0xc000885880) /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/application.go:244 +0x82 panic(0x8bde60, 0xc00039a160) /usr/lib/go/src/runtime/panic.go:967 +0x166 github.com/rivo/tview.(*TextView).reindexBuffer(0xc00143a000, 0x8c) /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/textview.go:719 +0x15bb github.com/rivo/tview.(*TextView).Draw(0xc00143a000, 0x99a5a0, 0xc000cbe1c0) /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/textview.go:861 +0x17b github.com/rivo/tview.(*Flex).Draw(0xc001804330, 0x99a5a0, 0xc000cbe1c0) /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/flex.go:183 +0x344 github.com/rivo/tview.(*Flex).Draw(0xc0018042d0, 0x99a5a0, 0xc000cbe1c0) /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/flex.go:183 +0x344 github.com/rivo/tview.(*Pages).Draw(0xc0018042a0, 0x99a5a0, 0xc000cbe1c0) /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/pages.go:278 +0xa8 github.com/rivo/tview.(*Application).draw(0xc000885880, 0x0) /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/application.go:582 +0xda github.com/rivo/tview.(*Application).Run(0xc000885880, 0x0, 0x0) /home/peter/go/pkg/mod/github.com/rivo/tview@v0.0.0-20200404204604-ca37f83cb2e7/application.go:332 +0x77d main.run(0xc000036a80, 0xc00000f300, 0x1) ```
kerem closed this issue 2026-03-04 01:03:52 +03:00
Author
Owner

@rivo commented on GitHub (Apr 14, 2020):

To determine the best solution to this, it would be helpful to be able to reproduce this panic. Can you please post a small program that will cause this panic?

<!-- gh-comment-id:613391318 --> @rivo commented on GitHub (Apr 14, 2020): To determine the best solution to this, it would be helpful to be able to reproduce this panic. Can you please post a small program that will cause this panic?
Author
Owner

@db-tech commented on GitHub (Apr 23, 2020):

I've got the same error. For me the main problem was that i wrongly sliced a string.
It took me a while but I could reproduce it:

func main() {
	var app = tview.NewApplication()
	bla := "├─XXXXXXXXX"
	textView := tview.NewTextView().SetWrap(true).SetText(bla[2:])
	textView.SetDrawFunc(func(screen tcell.Screen, x, y, width, height int) (int, int, int, int) {
		return x,y,10,height
	})
	if err := app.SetRoot(textView, true).EnableMouse(true).Run(); err != nil {
		panic(err)
	}
}

Seems a problem with those special character, the slicing and the wrapping.
I've no time at the moment but could take a look at it next week.

<!-- gh-comment-id:618639047 --> @db-tech commented on GitHub (Apr 23, 2020): I've got the same error. For me the main problem was that i wrongly sliced a string. It took me a while but I could reproduce it: ```go func main() { var app = tview.NewApplication() bla := "├─XXXXXXXXX" textView := tview.NewTextView().SetWrap(true).SetText(bla[2:]) textView.SetDrawFunc(func(screen tcell.Screen, x, y, width, height int) (int, int, int, int) { return x,y,10,height }) if err := app.SetRoot(textView, true).EnableMouse(true).Run(); err != nil { panic(err) } } ``` Seems a problem with those special character, the slicing and the wrapping. I've no time at the moment but could take a look at it next week.
Author
Owner

@rivo commented on GitHub (May 7, 2020):

Interesting. This appears to be a problem of converting strings to rune slices and back. If the original string contains invalid code points, the resulting string may be longer. See here:

https://play.golang.org/p/0PrzUP4_4cr

And that conversion is what happens in go-runewidth's Truncate() function which I'm using. I will submit a pull request over there.

<!-- gh-comment-id:625393756 --> @rivo commented on GitHub (May 7, 2020): Interesting. This appears to be a problem of converting strings to rune slices and back. If the original string contains invalid code points, the resulting string may be longer. See here: https://play.golang.org/p/0PrzUP4_4cr And that conversion is what happens in `go-runewidth`'s [`Truncate()`](https://github.com/mattn/go-runewidth/blob/f9aa72c7456cb586a8a4e22a73f2b1c6bce4ff54/runewidth.go#L148) function which I'm using. I will submit a pull request over there.
Author
Owner

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

My long-running PR to go-runewidth was just merged and the latest version does not produce a panic anymore with the example posted above. Unless you find that this is still a problem, I will soon close this issue. If there is still a panic, please post a small code snippet to reproduce.

(If you find that this issue is already closed, please open a new issue and reference this one.)

<!-- gh-comment-id:761839492 --> @rivo commented on GitHub (Jan 17, 2021): My long-running PR to `go-runewidth` was just merged and the latest version does not produce a panic anymore with the example posted above. Unless you find that this is still a problem, I will soon close this issue. If there is still a panic, please post a small code snippet to reproduce. (If you find that this issue is already closed, please open a new issue and reference this one.)
Author
Owner

@pengux commented on GitHub (Jan 17, 2021):

I haven't tried out your PR but let's close this issue and I can create a new one if needed.

<!-- gh-comment-id:761874600 --> @pengux commented on GitHub (Jan 17, 2021): I haven't tried out your PR but let's close this issue and I can create a new one if needed.
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#314
No description provided.