Skip to content

Conversation

@echobt
Copy link
Contributor

@echobt echobt commented Feb 4, 2026

Summary

This PR consolidates 7 individual TUI fixes into a single, cohesive change:

Included PRs:

Changes:

  • Added saturating casts for u16 conversions to prevent overflow
  • Fixed cursor positioning calculations in selection lists
  • Added bounds checking for empty sections in help browser
  • Improved Unicode width handling for popup positioning

Files Modified:

  • src/cortex-tui-components/src/dropdown.rs
  • src/cortex-tui-components/src/scroll.rs
  • src/cortex-tui-components/src/selection_list.rs
  • src/cortex-tui/src/cards/commands.rs
  • src/cortex-tui/src/cards/models.rs
  • src/cortex-tui/src/cards/sessions.rs
  • src/cortex-tui/src/interactive/renderer.rs
  • src/cortex-tui/src/widgets/autocomplete.rs
  • src/cortex-tui/src/widgets/help_browser/render.rs
  • src/cortex-tui/src/widgets/help_browser/state.rs
  • src/cortex-tui/src/widgets/help_browser/tests.rs
  • src/cortex-tui/src/widgets/mention_popup.rs
  • src/cortex-tui/src/widgets/scrollable_dropdown.rs

Closes #38, #42, #58, #59, #60, #64, #66

… prevention

This PR consolidates the following fixes:
- #38: Prevent usize to u16 overflow in interactive renderer
- #42: Prevent usize to u16 overflow in card count displays
- #58: Fix cursor positioning and underflow in selection list
- #59: Fix mention popup positioning and Unicode width calculation
- #60: Improve autocomplete popup positioning and width calculation
- #64: Prevent underflow in dropdown navigation and scroll calculations
- #66: Prevent panic in HelpBrowserState when sections empty

All changes target the TUI components to improve robustness:
- Added saturating casts for u16 conversions
- Fixed cursor positioning calculations
- Added bounds checking for empty sections
- Improved Unicode width handling for popups
@greptile-apps
Copy link

greptile-apps bot commented Feb 4, 2026

Greptile Overview

Greptile Summary

This PR consolidates 7 individual TUI fixes addressing overflow, underflow, and panic issues across dropdown, scroll, and popup components.

Key Changes:

  • Overflow Prevention: Replaced direct usize as u16 casts with u16::try_from().unwrap_or(u16::MAX) in card height calculations and interactive renderer to handle cases where item counts exceed 65,535
  • Underflow Prevention: Added comprehensive max_visible == 0 guards and double saturating_sub patterns (e.g., max_visible.saturating_sub(1)) throughout scroll offset calculations to prevent integer underflow
  • Panic Prevention: Changed HelpBrowserState::current_section() to return Option<&HelpSection> with empty sections guard, preventing index-out-of-bounds panics
  • Unicode Width Handling: Replaced .len() with .chars().count() in mention popup and cursor positioning for accurate multi-byte character support
  • Popup Positioning: Improved autocomplete and mention popup positioning with fits_above checks to prevent rendering off-screen

All changes use defensive programming patterns (saturating arithmetic, bounds checking, early returns) consistently applied across 13 files. Test coverage includes the new empty sections case.

Confidence Score: 5/5

  • Safe to merge - defensive fixes prevent crashes without changing functional behavior
  • All changes are defensive safety improvements using well-established Rust patterns (saturating arithmetic, Option types, bounds checking). Each fix targets a specific panic/overflow scenario with appropriate tests. No logic changes to core functionality.
  • No files require special attention

Important Files Changed

Filename Overview
src/cortex-tui-components/src/scroll.rs Added zero visible items guard and double saturating_sub to prevent underflow in scroll calculations
src/cortex-tui-components/src/selection_list.rs Fixed cursor positioning with character count and saturating arithmetic for disabled reason alignment
src/cortex-tui/src/widgets/autocomplete.rs Changed width calculation to use visible items instead of all items, and improved popup positioning fallback logic
src/cortex-tui/src/widgets/help_browser/state.rs Changed current_section() return type to Option<&HelpSection> with empty sections guard
src/cortex-tui/src/widgets/scrollable_dropdown.rs Added comprehensive zero max_visible guards and bounds checking with double saturating_sub throughout

Sequence Diagram

sequenceDiagram
    participant User
    participant TUI as TUI Component
    participant State as Component State
    participant Render as Render Logic
    
    User->>TUI: Navigate/Scroll Action
    TUI->>State: Update selection/scroll
    
    alt Empty or Zero Visible Items
        State->>State: Check max_visible == 0 || items.is_empty()
        State-->>TUI: Return early (prevent underflow)
    else Normal Case
        State->>State: Calculate new position
        State->>State: Use saturating_sub(max_visible.saturating_sub(1))
        State-->>TUI: Safe position value
    end
    
    TUI->>Render: Calculate dimensions
    
    alt Large Item Count
        Render->>Render: u16::try_from(count).unwrap_or(u16::MAX)
        Render->>Render: saturating_add for height components
        Render-->>TUI: Clamped u16 dimensions
    else Unicode/Wide Characters
        Render->>Render: Use chars().count() for width
        Render-->>TUI: Accurate Unicode-aware dimensions
    end
    
    alt Popup Positioning
        Render->>Render: Check if area.y >= height
        alt Fits Above
            Render->>Render: Position above input
        else No Room Above
            Render->>Render: Position below input
        end
    end
    
    TUI->>User: Display UI (no panic/overflow)
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@echobt
Copy link
Contributor Author

echobt commented Feb 4, 2026

Closing this PR to consolidate into a single mega-PR combining all bug fixes. The changes will be included in a new consolidated PR.

@echobt echobt closed this Feb 4, 2026
echobt added a commit that referenced this pull request Feb 4, 2026
This PR consolidates all bug fixes and security improvements from PRs #69-88 into a single cohesive change.

## Categories

### Security Fixes
- Path traversal prevention in MCP and session storage
- Shell injection prevention in restore scripts
- Secure random temp files for external editor
- TOCTOU race condition fixes

### TUI Improvements
- Overflow prevention for u16 conversions
- Cursor positioning fixes in selection lists
- Unicode width handling for popups
- Empty section handling in help browser

### Error Handling
- Graceful semaphore and init failure handling
- Improved error propagation in middleware
- Better client access error handling
- SystemTime operation safety

### Memory and Storage
- Cache size limits to prevent unbounded growth
- File lock cleanup for memory leak prevention
- fsync after critical writes for durability
- Bounded ToolResponseStore with automatic cleanup

### Protocol Robustness
- Buffer size limits for StreamProcessor
- ToolState transition validation
- State machine documentation

### Numeric Safety
- Saturating operations to prevent overflow/underflow
- Safe UTF-8 string slicing throughout codebase

### Tools
- Parameter alias support for backward compatibility
- Handler name consistency fixes

## Files Modified
Multiple files across cortex-tui, cortex-engine, cortex-exec, cortex-common,
cortex-protocol, cortex-storage, cortex-mcp-server, and other crates.

Closes #69, #70, #71, #73, #75, #80, #82, #87, #88
echobt added a commit that referenced this pull request Feb 4, 2026
This PR consolidates all bug fixes and security improvements from PRs #69-88 into a single cohesive change.

## Categories

### Security Fixes
- Path traversal prevention in MCP and session storage
- Shell injection prevention in restore scripts
- Secure random temp files for external editor
- TOCTOU race condition fixes

### TUI Improvements
- Overflow prevention for u16 conversions
- Cursor positioning fixes in selection lists
- Unicode width handling for popups
- Empty section handling in help browser

### Error Handling
- Graceful semaphore and init failure handling
- Improved error propagation in middleware
- Better client access error handling
- SystemTime operation safety

### Memory and Storage
- Cache size limits to prevent unbounded growth
- File lock cleanup for memory leak prevention
- fsync after critical writes for durability
- Bounded ToolResponseStore with automatic cleanup

### Protocol Robustness
- Buffer size limits for StreamProcessor
- ToolState transition validation
- State machine documentation

### Numeric Safety
- Saturating operations to prevent overflow/underflow
- Safe UTF-8 string slicing throughout codebase

### Tools
- Parameter alias support for backward compatibility
- Handler name consistency fixes

## Files Modified
Multiple files across cortex-tui, cortex-engine, cortex-exec, cortex-common,
cortex-protocol, cortex-storage, cortex-mcp-server, and other crates.

Closes #69, #70, #71, #73, #75, #80, #82, #87, #88
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant