terminal: Prevent scrollbar arithmetic underflow panic#45282
Merged
P1n3appl3 merged 2 commits intozed-industries:mainfrom Dec 19, 2025
Merged
Conversation
Standardize all subtraction operations in terminal_scrollbar.rs to use `saturating_sub` instead of raw subtraction or `checked_sub().unwrap_or(0)`. This prevents panics when `total_lines < viewport_lines + display_offset`, which can occur during terminal creation, small window sizes, or when display_offset becomes stale. Closes zed-industries#45281
Contributor
3 tasks
rtfeldman
pushed a commit
that referenced
this pull request
Jan 5, 2026
## Summary Fixes arithmetic underflow panics in `terminal_scrollbar.rs` by converting unsafe subtractions to `saturating_sub`. Closes #45281 ## Problem Two locations perform raw subtraction on `usize` values that panic when underflow occurs: - `offset()`: `state.total_lines - state.viewport_lines - state.display_offset` - `set_offset()`: `state.total_lines - state.viewport_lines` This happens when `total_lines < viewport_lines + display_offset`, which can occur during terminal creation, with small window sizes, or when display state becomes stale. ## Solution Replace the two unsafe subtractions with `saturating_sub`, which returns 0 on underflow instead of panicking. Also standardizes the existing `checked_sub().unwrap_or(0)` in `max_offset()` to `saturating_sub` for consistency across the file. ## Changes - N/A
LivioGama
pushed a commit
to LivioGama/zed
that referenced
this pull request
Jan 20, 2026
…s#45282) ## Summary Fixes arithmetic underflow panics in `terminal_scrollbar.rs` by converting unsafe subtractions to `saturating_sub`. Closes zed-industries#45281 ## Problem Two locations perform raw subtraction on `usize` values that panic when underflow occurs: - `offset()`: `state.total_lines - state.viewport_lines - state.display_offset` - `set_offset()`: `state.total_lines - state.viewport_lines` This happens when `total_lines < viewport_lines + display_offset`, which can occur during terminal creation, with small window sizes, or when display state becomes stale. ## Solution Replace the two unsafe subtractions with `saturating_sub`, which returns 0 on underflow instead of panicking. Also standardizes the existing `checked_sub().unwrap_or(0)` in `max_offset()` to `saturating_sub` for consistency across the file. ## Changes - N/A
LivioGama
pushed a commit
to LivioGama/zed
that referenced
this pull request
Jan 20, 2026
…s#45282) ## Summary Fixes arithmetic underflow panics in `terminal_scrollbar.rs` by converting unsafe subtractions to `saturating_sub`. Closes zed-industries#45281 ## Problem Two locations perform raw subtraction on `usize` values that panic when underflow occurs: - `offset()`: `state.total_lines - state.viewport_lines - state.display_offset` - `set_offset()`: `state.total_lines - state.viewport_lines` This happens when `total_lines < viewport_lines + display_offset`, which can occur during terminal creation, with small window sizes, or when display state becomes stale. ## Solution Replace the two unsafe subtractions with `saturating_sub`, which returns 0 on underflow instead of panicking. Also standardizes the existing `checked_sub().unwrap_or(0)` in `max_offset()` to `saturating_sub` for consistency across the file. ## Changes - N/A
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes arithmetic underflow panics in
terminal_scrollbar.rsby converting unsafe subtractions tosaturating_sub.Closes #45281
Problem
Two locations perform raw subtraction on
usizevalues that panic when underflow occurs:offset():state.total_lines - state.viewport_lines - state.display_offsetset_offset():state.total_lines - state.viewport_linesThis happens when
total_lines < viewport_lines + display_offset, which can occur during terminal creation, with small window sizes, or when display state becomes stale.Solution
Replace the two unsafe subtractions with
saturating_sub, which returns 0 on underflow instead of panicking.Also standardizes the existing
checked_sub().unwrap_or(0)inmax_offset()tosaturating_subfor consistency across the file.Changes
crates/terminal_view/src/terminal_scrollbar.rs: 3 subtraction operations now usesaturating_sub