[GH-ISSUE #262] TreeView panics with 'index out of range' error #203

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

Originally created by @urandom on GitHub (Apr 6, 2019).
Original GitHub issue: https://github.com/rivo/tview/issues/262

For some reason, during TreeView's process, the code tries to get a node outside the index. The problem is that the tree does not have any nodes yet.

The stack trace:

github.com/rivo/tview.(*Application).Run.func1(0xc000268f00)
	/home/urandom/.go/src/github.com/rivo/tview/application.go:149 +0x82
panic(0x114ba80, 0x1e28b80)
	/usr/lib/go/src/runtime/panic.go:522 +0x1b5
github.com/rivo/tview.(*TreeView).process(0xc000269000)
	/home/urandom/.go/src/github.com/rivo/tview/treeview.go:501 +0x432
github.com/rivo/tview.(*TreeView).Draw(0xc000269000, 0x146c160, 0xc0002416c0)
	/home/urandom/.go/src/github.com/rivo/tview/treeview.go:543 +0x931
github.com/rivo/tview.(*Flex).Draw(0xc0002cf0b0, 0x146c160, 0xc0002416c0)
	/home/urandom/.go/src/github.com/rivo/tview/flex.go:169 +0x290
github.com/rivo/tview.(*Flex).Draw(0xc0002cf050, 0x146c160, 0xc0002416c0)
	/home/urandom/.go/src/github.com/rivo/tview/flex.go:169 +0x290
github.com/rivo/tview.(*Pages).Draw(0xc0002cefc0, 0x146c160, 0xc0002416c0)
	/home/urandom/.go/src/github.com/rivo/tview/pages.go:246 +0xf1
github.com/rivo/tview.(*Application).draw(0xc000268f00, 0x0)
	/home/urandom/.go/src/github.com/rivo/tview/application.go:363 +0xd8
github.com/rivo/tview.(*Application).QueueUpdateDraw.func1()
	/home/urandom/.go/src/github.com/rivo/tview/application.go:494 +0x3d
github.com/rivo/tview.(*Application).Run(0xc000268f00, 0x0, 0x0)
	/home/urandom/.go/src/github.com/rivo/tview/application.go:253 +0x4dd

The simplest fix might be to return early from the process method if there are no nodes.

Originally created by @urandom on GitHub (Apr 6, 2019). Original GitHub issue: https://github.com/rivo/tview/issues/262 For some reason, during TreeView's process, the code tries to get a node outside the index. The problem is that the tree does not have any nodes yet. The stack trace: ``` github.com/rivo/tview.(*Application).Run.func1(0xc000268f00) /home/urandom/.go/src/github.com/rivo/tview/application.go:149 +0x82 panic(0x114ba80, 0x1e28b80) /usr/lib/go/src/runtime/panic.go:522 +0x1b5 github.com/rivo/tview.(*TreeView).process(0xc000269000) /home/urandom/.go/src/github.com/rivo/tview/treeview.go:501 +0x432 github.com/rivo/tview.(*TreeView).Draw(0xc000269000, 0x146c160, 0xc0002416c0) /home/urandom/.go/src/github.com/rivo/tview/treeview.go:543 +0x931 github.com/rivo/tview.(*Flex).Draw(0xc0002cf0b0, 0x146c160, 0xc0002416c0) /home/urandom/.go/src/github.com/rivo/tview/flex.go:169 +0x290 github.com/rivo/tview.(*Flex).Draw(0xc0002cf050, 0x146c160, 0xc0002416c0) /home/urandom/.go/src/github.com/rivo/tview/flex.go:169 +0x290 github.com/rivo/tview.(*Pages).Draw(0xc0002cefc0, 0x146c160, 0xc0002416c0) /home/urandom/.go/src/github.com/rivo/tview/pages.go:246 +0xf1 github.com/rivo/tview.(*Application).draw(0xc000268f00, 0x0) /home/urandom/.go/src/github.com/rivo/tview/application.go:363 +0xd8 github.com/rivo/tview.(*Application).QueueUpdateDraw.func1() /home/urandom/.go/src/github.com/rivo/tview/application.go:494 +0x3d github.com/rivo/tview.(*Application).Run(0xc000268f00, 0x0, 0x0) /home/urandom/.go/src/github.com/rivo/tview/application.go:253 +0x4dd ``` The simplest fix might be to return early from the process method if there are no nodes.
kerem closed this issue 2026-03-04 01:02:57 +03:00
Author
Owner

@rivo commented on GitHub (Apr 6, 2019):

I'm having trouble following this stack trace. The line numbers don't seem to line up with the current code. And TreeView actually doesn't do anything if there is no root node, see here:

github.com/rivo/tview@9d616aee87/treeview.go (L542)

Are you using the latest commit?

<!-- gh-comment-id:480511289 --> @rivo commented on GitHub (Apr 6, 2019): I'm having trouble following this stack trace. The line numbers don't seem to line up with the current code. And `TreeView` actually doesn't do anything if there is no root node, see here: https://github.com/rivo/tview/blob/9d616aee870d6dde6c48a530fc1de0c82602afed/treeview.go#L542 Are you using the latest commit?
Author
Owner

@urandom commented on GitHub (Apr 7, 2019):

This is an up-to-date stack trace:

panic: runtime error: index out of range [recovered]
	panic: runtime error: index out of range

goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1(0xc000234d80)
	/home/urandom/.go/src/github.com/rivo/tview/application.go:149 +0x82
panic(0x1151140, 0x1e16ae0)
	/usr/lib/go/src/runtime/panic.go:522 +0x1b5
github.com/rivo/tview.(*TreeView).process(0xc000234e80)
	/home/urandom/.go/src/github.com/rivo/tview/treeview.go:506 +0x432
github.com/rivo/tview.(*TreeView).Draw(0xc000234e80, 0x1471320, 0xc000115340)
	/home/urandom/.go/src/github.com/rivo/tview/treeview.go:548 +0x931
github.com/rivo/tview.(*Flex).Draw(0xc00027aa80, 0x1471320, 0xc000115340)
	/home/urandom/.go/src/github.com/rivo/tview/flex.go:169 +0x290
github.com/rivo/tview.(*Flex).Draw(0xc00027aa20, 0x1471320, 0xc000115340)
	/home/urandom/.go/src/github.com/rivo/tview/flex.go:169 +0x290
github.com/rivo/tview.(*Pages).Draw(0xc00027a990, 0x1471320, 0xc000115340)
	/home/urandom/.go/src/github.com/rivo/tview/pages.go:262 +0xf1
github.com/rivo/tview.(*Application).draw(0xc000234d80, 0x0)
	/home/urandom/.go/src/github.com/rivo/tview/application.go:363 +0xd8
github.com/rivo/tview.(*Application).QueueUpdateDraw.func1()
	/home/urandom/.go/src/github.com/rivo/tview/application.go:494 +0x3d
github.com/rivo/tview.(*Application).Run(0xc000234d80, 0x0, 0x0)
	/home/urandom/.go/src/github.com/rivo/tview/application.go:253 +0x4dd
<!-- gh-comment-id:480578622 --> @urandom commented on GitHub (Apr 7, 2019): This is an up-to-date stack trace: ``` panic: runtime error: index out of range [recovered] panic: runtime error: index out of range goroutine 1 [running]: github.com/rivo/tview.(*Application).Run.func1(0xc000234d80) /home/urandom/.go/src/github.com/rivo/tview/application.go:149 +0x82 panic(0x1151140, 0x1e16ae0) /usr/lib/go/src/runtime/panic.go:522 +0x1b5 github.com/rivo/tview.(*TreeView).process(0xc000234e80) /home/urandom/.go/src/github.com/rivo/tview/treeview.go:506 +0x432 github.com/rivo/tview.(*TreeView).Draw(0xc000234e80, 0x1471320, 0xc000115340) /home/urandom/.go/src/github.com/rivo/tview/treeview.go:548 +0x931 github.com/rivo/tview.(*Flex).Draw(0xc00027aa80, 0x1471320, 0xc000115340) /home/urandom/.go/src/github.com/rivo/tview/flex.go:169 +0x290 github.com/rivo/tview.(*Flex).Draw(0xc00027aa20, 0x1471320, 0xc000115340) /home/urandom/.go/src/github.com/rivo/tview/flex.go:169 +0x290 github.com/rivo/tview.(*Pages).Draw(0xc00027a990, 0x1471320, 0xc000115340) /home/urandom/.go/src/github.com/rivo/tview/pages.go:262 +0xf1 github.com/rivo/tview.(*Application).draw(0xc000234d80, 0x0) /home/urandom/.go/src/github.com/rivo/tview/application.go:363 +0xd8 github.com/rivo/tview.(*Application).QueueUpdateDraw.func1() /home/urandom/.go/src/github.com/rivo/tview/application.go:494 +0x3d github.com/rivo/tview.(*Application).Run(0xc000234d80, 0x0, 0x0) /home/urandom/.go/src/github.com/rivo/tview/application.go:253 +0x4dd ```
Author
Owner

@urandom commented on GitHub (Apr 7, 2019):

On further investigation there is at least one node in the tree at this point, and the tree is set to level 1, and has finished being updated with more nodes via a QueueUpdateDraw call (which causes the Draw call)

The panic itself is caused by the root node being set as current (via SetCurrentNode), while also being invisible, and not having any children

<!-- gh-comment-id:480578940 --> @urandom commented on GitHub (Apr 7, 2019): On further investigation there is at least one node in the tree at this point, and the tree is set to level 1, and has finished being updated with more nodes via a QueueUpdateDraw call (which causes the Draw call) The panic itself is caused by the root node being set as current (via SetCurrentNode), while also being invisible, and not having any children
Author
Owner

@rivo commented on GitHub (May 13, 2019):

I made a change which I believe will fix the panic that you reported. Please have a look and let me know if it works now.

<!-- gh-comment-id:491846291 --> @rivo commented on GitHub (May 13, 2019): I made a change which I believe will fix the panic that you reported. Please have a look and let me know if it works now.
Author
Owner

@urandom commented on GitHub (May 14, 2019):

I can't seem to reproduce the problem with your latest change.

<!-- gh-comment-id:492152773 --> @urandom commented on GitHub (May 14, 2019): I can't seem to reproduce the problem with your latest change.
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#203
No description provided.