-
-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Labels
area:integrations/terminalFeedback for terminal integration, shell commands, etcFeedback for terminal integration, shell commands, etcfrequency:commonBugs that happen for at least a third of the users across all platforms and kinds of usageBugs that happen for at least a third of the users across all platforms and kinds of usagepriority:P2Average run-of-the-mill bugsAverage run-of-the-mill bugsstate:needs reproNeeds reproduction steps / someone to reproduceNeeds reproduction steps / someone to reproduce
Description
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_offsetbecomes stale relative tototal_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).
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area:integrations/terminalFeedback for terminal integration, shell commands, etcFeedback for terminal integration, shell commands, etcfrequency:commonBugs that happen for at least a third of the users across all platforms and kinds of usageBugs that happen for at least a third of the users across all platforms and kinds of usagepriority:P2Average run-of-the-mill bugsAverage run-of-the-mill bugsstate:needs reproNeeds reproduction steps / someone to reproduceNeeds reproduction steps / someone to reproduce