# terminal: Prevent scrollbar arithmetic underflow panic \(\#45282\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/e052127e1c31ce6b8286fc931aaee2217b62ada1)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: e052127e1c31ce6b8286fc931aaee2217b62ada1

Requested commit: e052127e1c31ce6b8286fc931aaee2217b62ada1

Commit: e052127e1c31ce6b8286fc931aaee2217b62ada1

Tree: d3d5f5824697e0cb152c4c7e5cf1457165a41e46

Author: rabsef-bicrym

Committer: GitHub

## Message

```
terminal: Prevent scrollbar arithmetic underflow panic (#45282)

## 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
```

## Parents

- [0531035b86a7a035b1b661c62cc9436a2e1b5394](https://git.cafe/gitcafe/zed/commit/0531035b86a7a035b1b661c62cc9436a2e1b5394?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/e052127e1c31ce6b8286fc931aaee2217b62ada1?format=markdown)
