Skip to content

Critical: Fix bounds access panics and performance issues in help menu search + Add unit tests #1025

@coderabbitai

Description

@coderabbitai

Problem Description

Critical bugs identified in PR #1011 (help menu search functionality) that need immediate attention:

1. CRITICAL: Bounds Access Panic Risk

Location: src/internal/handle_modal.go lines 149 and 153 in helpMenuListUp()

Issue: Direct slice access without bounds validation:

if m.helpMenu.filteredData[m.helpMenu.cursor].subTitle != "" {  // Line 149 - PANIC RISK
    m.helpMenu.renderIndex--
}
if m.helpMenu.filteredData[m.helpMenu.cursor].subTitle != "" {  // Line 153 - PANIC RISK  
    m.helpMenu.cursor--
}

Impact: Application crash when filteredData is empty or cursor is out of bounds.

2. SEVERE: Performance Degradation

Issues:

  • Multiple len(m.helpMenu.filteredData) calls (6+ times per navigation)
  • Rendering recalculates key lengths on every frame
  • No input debouncing for rapid typing
  • Redundant calculations in render loop

3. Race Conditions (Future Consideration)

Navigation functions perform multiple operations on filteredData without atomic checks. While not currently an issue due to no concurrent access, should be considered for future improvements.

Required Fixes

Immediate (Critical):

  1. Add bounds checking before all slice access operations:
func (m *model) helpMenuListUp() {
    if len(m.helpMenu.filteredData) == 0 {
        return
    }
    if m.helpMenu.cursor < 0 || m.helpMenu.cursor >= len(m.helpMenu.filteredData) {
        m.helpMenu.cursor = 1 // Reset to safe position
        return
    }
    // ... rest with safe access
}
  1. Cache length calculations to avoid repeated calls
  2. Add state validation to ensure cursor/renderIndex consistency

Unit Testing Coverage Needed:

  • TestHelpMenuListUpEmptyData
  • TestHelpMenuListDownEmptyData
  • TestHelpMenuNavigationWithInvalidCursor
  • TestFuzzySearchEmptyInput
  • TestFuzzySearchSpecialCharacters
  • TestRemoveOrphanSectionsAllRemoved
  • TestFilterHelpMenuStateConsistency
  • TestSearchBarFocusTransitions

Manual Testing Requirements

  • Search queries that match nothing
  • Navigate in empty filtered results
  • Rapid type/delete in search bar
  • Navigate while typing
  • Resize terminal during search

References

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions