Add bounds checking and defensive programming improvements

Co-authored-by: foglar <82380203+foglar@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-10-09 09:09:27 +00:00
parent cfedfd3550
commit 0e79ba9083
3 changed files with 16 additions and 3 deletions

View File

@ -56,7 +56,7 @@ func updateChatView(v *gocui.View) {
h = 0
}
if len(users) == 0 {
if len(users) == 0 || selectedUserIdx >= len(users) {
continue
}
@ -72,7 +72,7 @@ func updateChatView(v *gocui.View) {
}
func sendMessage(g *gocui.Gui, v *gocui.View) error {
if len(users) == 0 {
if len(users) == 0 || selectedUserIdx >= len(users) {
return nil
}

View File

@ -33,14 +33,22 @@ func updateContactsView(g *gocui.Gui) error {
return errors.New("no contacts in the list, find some friends")
}
// Ensure selectedUserIdx is within bounds
if selectedUserIdx < 0 || selectedUserIdx >= len(users) {
selectedUserIdx = 0
}
LoadMessages(users[selectedUserIdx])
_, maxY := g.Size()
h := min(len(users), (maxY/2)-1)
if h <= 0 {
h = 1
}
startI := max(0, min(selectedUserIdx-(h/2), len(users)-h))
fmt.Fprint(v, "\n\n")
for i := startI; i < startI+h; i++ {
for i := startI; i < startI+h && i < len(users); i++ {
u := users[i]
fmt.Fprint(v, "\t\t\t\t")

View File

@ -14,6 +14,11 @@ var selectedUserIdx int = 0
func Run() {
LoadContacts(defaultServerPath)
// Load initial messages if there are any contacts
if len(users) > 0 && selectedUserIdx < len(users) {
LoadMessages(users[selectedUserIdx])
}
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Panicln(err)