[GH-ISSUE #959] Panic in MouseHandler for TextView with borders #705

Closed
opened 2026-03-04 01:07:09 +03:00 by kerem · 1 comment
Owner

Originally created by @Macmod on GitHub (Mar 14, 2024).
Original GitHub issue: https://github.com/rivo/tview/issues/959

It seems that https://github.com/rivo/tview/blob/master/textview.go#L1386 is missing a bounds check for when y+t.lineOffset is negative, which leads to a panic in this scenario, when the user clicks on the top border instead of the text itself:

Sample

package main

import (
        "fmt"
        "log"

        "github.com/rivo/tview"
)

func main() {
        tabs := tview.NewTextView().SetRegions(true)
        tabs.SetBorder(true)

        fmt.Fprintf(tabs, `["%s"][white]%s[black][""] `, "0", "Anything")

        myApp := tview.NewApplication()
        myApp.EnableMouse(true)

        myFlex := tview.NewFlex().SetDirection(tview.FlexRow).
                AddItem(
                        tview.NewFlex().
                                AddItem(tabs, 20, 0, false),
                        3, 0, false,
                )

        if err := myApp.SetRoot(myFlex, true).Run(); err != nil {
                log.Fatal(err)
        }
}

Trace

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

goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1()
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/application.go:285 +0x45
panic({0x545560?, 0xc00012e000?})
        /usr/local/go/src/runtime/panic.go:914 +0x21f
github.com/rivo/tview.(*TextView).MouseHandler.func1(0x3, 0x18?, 0xc000116310)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/textview.go:1386 +0x32e
github.com/rivo/tview.(*TextView).MouseHandler.(*Box).WrapMouseHandler.func2(0xa000?, 0xc000100000?, 0xc000118318?)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/box.go:226 +0x58
github.com/rivo/tview.(*Flex).MouseHandler.func1(0x3347?, 0xc0001142a0, 0x18?)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/flex.go:239 +0xc3
github.com/rivo/tview.(*Flex).MouseHandler.(*Box).WrapMouseHandler.func2(0xc630?, 0x10?, 0x538760?)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/box.go:226 +0x58
github.com/rivo/tview.(*Flex).MouseHandler.func1(0x5b78?, 0xc0001142a0, 0x10?)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/flex.go:239 +0xc3
github.com/rivo/tview.(*Flex).MouseHandler.(*Box).WrapMouseHandler.func2(0xc600?, 0x43798e?, 0x1?)        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/box.go:226 +0x58
github.com/rivo/tview.(*Application).fireMouseActions.func1(0x6500?)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/application.go:522 +0x182
github.com/rivo/tview.(*Application).fireMouseActions(0xc00018e000, 0xc0001142a0)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/application.go:559 +0x2c9
github.com/rivo/tview.(*Application).Run(0xc00018e000)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/application.go:458 +0xbc5
main.main()
        /home/macmod/Code/tview-issue/test.go:26 +0x397
Originally created by @Macmod on GitHub (Mar 14, 2024). Original GitHub issue: https://github.com/rivo/tview/issues/959 It seems that https://github.com/rivo/tview/blob/master/textview.go#L1386 is missing a bounds check for when `y+t.lineOffset` is negative, which leads to a panic in this scenario, when the user clicks on the top border instead of the text itself: **Sample** ```golang package main import ( "fmt" "log" "github.com/rivo/tview" ) func main() { tabs := tview.NewTextView().SetRegions(true) tabs.SetBorder(true) fmt.Fprintf(tabs, `["%s"][white]%s[black][""] `, "0", "Anything") myApp := tview.NewApplication() myApp.EnableMouse(true) myFlex := tview.NewFlex().SetDirection(tview.FlexRow). AddItem( tview.NewFlex(). AddItem(tabs, 20, 0, false), 3, 0, false, ) if err := myApp.SetRoot(myFlex, true).Run(); err != nil { log.Fatal(err) } } ``` **Trace** ```bash panic: runtime error: index out of range [-1] [recovered] panic: runtime error: index out of range [-1] goroutine 1 [running]: github.com/rivo/tview.(*Application).Run.func1() /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/application.go:285 +0x45 panic({0x545560?, 0xc00012e000?}) /usr/local/go/src/runtime/panic.go:914 +0x21f github.com/rivo/tview.(*TextView).MouseHandler.func1(0x3, 0x18?, 0xc000116310) /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/textview.go:1386 +0x32e github.com/rivo/tview.(*TextView).MouseHandler.(*Box).WrapMouseHandler.func2(0xa000?, 0xc000100000?, 0xc000118318?) /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/box.go:226 +0x58 github.com/rivo/tview.(*Flex).MouseHandler.func1(0x3347?, 0xc0001142a0, 0x18?) /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/flex.go:239 +0xc3 github.com/rivo/tview.(*Flex).MouseHandler.(*Box).WrapMouseHandler.func2(0xc630?, 0x10?, 0x538760?) /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/box.go:226 +0x58 github.com/rivo/tview.(*Flex).MouseHandler.func1(0x5b78?, 0xc0001142a0, 0x10?) /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/flex.go:239 +0xc3 github.com/rivo/tview.(*Flex).MouseHandler.(*Box).WrapMouseHandler.func2(0xc600?, 0x43798e?, 0x1?) /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/box.go:226 +0x58 github.com/rivo/tview.(*Application).fireMouseActions.func1(0x6500?) /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/application.go:522 +0x182 github.com/rivo/tview.(*Application).fireMouseActions(0xc00018e000, 0xc0001142a0) /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/application.go:559 +0x2c9 github.com/rivo/tview.(*Application).Run(0xc00018e000) /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/application.go:458 +0xbc5 main.main() /home/macmod/Code/tview-issue/test.go:26 +0x397 ```
kerem closed this issue 2026-03-04 01:07:09 +03:00
Author
Owner

@rivo commented on GitHub (Apr 3, 2024):

Fixed with #963

<!-- gh-comment-id:2034748573 --> @rivo commented on GitHub (Apr 3, 2024): Fixed with #963
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#705
No description provided.