Skip to content

Terminal scrollbar panics on arithmetic underflow #45281

@rabsef-bicrym

Description

@rabsef-bicrym

Summary

The terminal scrollbar implementation in terminal_scrollbar.rs can panic due to arithmetic underflow when total_lines < viewport_lines + display_offset.

Root Cause

Three locations use unsafe subtraction on usize values that can underflow:

offset() - line 63:

let scroll_offset = state.total_lines - state.viewport_lines - state.display_offset;

set_offset() - line 74:

let max_offset = state.total_lines - state.viewport_lines;

max_offset() - lines 53-56:

state
    .total_lines
    .checked_sub(state.viewport_lines)
    .unwrap_or(0)

(This one uses checked_sub but inconsistently with the others.)

When It Happens

  • Terminal is first created (before content fills the viewport)
  • Terminal is resized very small
  • display_offset becomes stale relative to total_lines

Proposed Fix

Standardize all three locations to use saturating_sub, which returns 0 on underflow instead of panicking:

// max_offset()
state.total_lines.saturating_sub(state.viewport_lines)

// offset()
state.total_lines
    .saturating_sub(state.viewport_lines)
    .saturating_sub(state.display_offset)

// set_offset()
let max_offset = state.total_lines.saturating_sub(state.viewport_lines);

This also standardizes the approach (replacing the mixed checked_sub().unwrap_or(0) pattern).

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:integrations/terminalFeedback for terminal integration, shell commands, etcfrequency:commonBugs that happen for at least a third of the users across all platforms and kinds of usagepriority:P2Average run-of-the-mill bugsstate:needs reproNeeds reproduction steps / someone to reproduce

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions