Skip to content

terminal: Prevent scrollbar arithmetic underflow panic#45282

Merged
P1n3appl3 merged 2 commits intozed-industries:mainfrom
rabsef-bicrym:fix/terminal-scrollbar-saturating-sub
Dec 19, 2025
Merged

terminal: Prevent scrollbar arithmetic underflow panic#45282
P1n3appl3 merged 2 commits intozed-industries:mainfrom
rabsef-bicrym:fix/terminal-scrollbar-saturating-sub

Conversation

@rabsef-bicrym
Copy link
Contributor

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

  • crates/terminal_view/src/terminal_scrollbar.rs: 3 subtraction operations now use saturating_sub

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
@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Dec 18, 2025
@maxdeviant maxdeviant changed the title fix(terminal): Prevent scrollbar arithmetic underflow panic terminal: Prevent scrollbar arithmetic underflow panic Dec 18, 2025
@P1n3appl3 P1n3appl3 self-assigned this Dec 18, 2025
@P1n3appl3 P1n3appl3 enabled auto-merge (squash) December 18, 2025 18:58
@zed-industries-bot
Copy link
Contributor

zed-industries-bot commented Dec 18, 2025

Warnings
⚠️

This PR is missing release notes.

Please add a "Release Notes" section that describes the change:

Release Notes:

- Added/Fixed/Improved ...

If your change is not user-facing, you can use "N/A" for the entry:

Release Notes:

- N/A

Generated by 🚫 dangerJS against c758a29

@P1n3appl3 P1n3appl3 merged commit e052127 into zed-industries:main Dec 19, 2025
24 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Terminal scrollbar panics on arithmetic underflow

3 participants