Skip to content

Conversation

@echobt
Copy link
Contributor

@echobt echobt commented Feb 4, 2026

Summary

Fixes #5166, #5159, #5141 - Card count displays overflow on large values.

Problem

Direct usize to u16 casts truncate when counts exceed 65535.

Solution

Used saturating conversion to cap at u16::MAX.

@greptile-apps
Copy link

greptile-apps bot commented Feb 4, 2026

Greptile Overview

Greptile Summary

This PR fixes overflow issues in card display height calculations and UTF-8 string slicing panics.

TUI Card Changes (3 files)

  • Replaced direct usize as u16 casts with safe u16::try_from().unwrap_or(u16::MAX) conversion
  • Added saturating_add() when adding padding to prevent arithmetic overflow
  • Affects desired_height() calculations in commands, models, and sessions cards

Mention Parsing Changes

  • Added safe_slice_up_to() and safe_slice_from() helper functions to handle UTF-8 character boundaries
  • Prevents panics when slicing strings at invalid byte positions (inside multibyte characters)
  • Includes comprehensive test coverage for ASCII, CJK characters, and emojis

Confidence Score: 5/5

  • Safe to merge - defensive programming fixes with comprehensive tests
  • All changes use proper Rust safety patterns (try_from, saturating operations, UTF-8 boundary checks) and include extensive test coverage for edge cases
  • No files require special attention

Important Files Changed

Filename Overview
src/cortex-tui/src/cards/commands.rs replaced unsafe as u16 cast with safe try_from().unwrap_or(u16::MAX) and added saturating_add for overflow protection
src/cortex-tui/src/cards/models.rs replaced unsafe as u16 cast with safe try_from().unwrap_or(u16::MAX) and added saturating_add for overflow protection
src/cortex-tui/src/cards/sessions.rs replaced unsafe as u16 cast with safe try_from().unwrap_or(u16::MAX) and added saturating_add for overflow protection
src/cortex-agents/src/mention.rs added UTF-8 boundary validation helpers (safe_slice_up_to, safe_slice_from) to prevent panics when slicing multibyte characters, with comprehensive tests

Sequence Diagram

sequenceDiagram
    participant User
    participant TUI as TUI Cards
    participant Conversion as Type Conversion
    participant Display as Display Rendering
    
    User->>TUI: View card (commands/models/sessions)
    TUI->>TUI: Get collection length (usize)
    TUI->>Conversion: u16::try_from(len)
    alt len <= u16::MAX
        Conversion-->>TUI: Ok(len as u16)
    else len > u16::MAX
        Conversion-->>TUI: Err(_)
        TUI->>TUI: Use u16::MAX instead
    end
    TUI->>TUI: saturating_add(count, padding)
    TUI->>Display: Calculate desired_height
    Display-->>User: Render card with safe height
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.

4 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

echobt added a commit that referenced this pull request Feb 4, 2026
… 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
@echobt
Copy link
Contributor Author

echobt commented Feb 4, 2026

Consolidated into #69 - fix(tui): consolidated TUI fixes for overflow, positioning, and panic prevention

@echobt echobt closed this Feb 4, 2026
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