Skip to content

buffer_diff: Fix panic when staging hunks with stale buffer snapshot#51641

Merged
Veykril merged 1 commit intomainfrom
veykril/push-yqosrxsyzlkr
Mar 16, 2026
Merged

buffer_diff: Fix panic when staging hunks with stale buffer snapshot#51641
Veykril merged 1 commit intomainfrom
veykril/push-yqosrxsyzlkr

Conversation

@Veykril
Copy link
Copy Markdown
Member

@Veykril Veykril commented Mar 16, 2026

When the buffer is edited after the diff is computed but before staging, anchor positions shift while diff_base_byte_range values don't. If the primary (HEAD) hunk extends past the unstaged (index) hunk, an edit in the extension region causes the overshoot calculation to produce an index_end that exceeds index_text.len(), panicking in rope::Cursor::suffix.

Fix by clamping index_end to index_text.len(). This is safe because the computed index text is an optimistic approximation — the real staging happens at the filesystem level via git add/git reset.

Closes ZED-5R2

Release Notes:

  • Fixed a source of panics when staging diff hunks

When the buffer is edited after the diff is computed but before staging,
anchor positions shift while diff_base_byte_range values don't. If the
primary (HEAD) hunk extends past the unstaged (index) hunk, an edit in
the extension region causes the overshoot calculation to produce an
index_end that exceeds index_text.len(), panicking in rope::Cursor::suffix.

Fix by clamping index_end to index_text.len(). This is safe because the
computed index text is an optimistic approximation — the real staging
happens at the filesystem level via git add/git reset.

Closes ZED-5R2
@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Mar 16, 2026
@zed-community-bot zed-community-bot bot added the staff Pull requests authored by a current member of Zed staff label Mar 16, 2026
@zed-codeowner-coordinator zed-codeowner-coordinator bot requested review from a team, cole-miller and osiewicz and removed request for a team March 16, 2026 08:19
@Veykril
Copy link
Copy Markdown
Member Author

Veykril commented Mar 16, 2026

/cherry-pick preview

@Veykril Veykril merged commit ebd80d7 into main Mar 16, 2026
40 checks passed
@Veykril Veykril deleted the veykril/push-yqosrxsyzlkr branch March 16, 2026 08:49
github-actions bot pushed a commit that referenced this pull request Mar 16, 2026
…51641)

When the buffer is edited after the diff is computed but before staging,
anchor positions shift while diff_base_byte_range values don't. If the
primary (HEAD) hunk extends past the unstaged (index) hunk, an edit in
the extension region causes the overshoot calculation to produce an
index_end that exceeds index_text.len(), panicking in
rope::Cursor::suffix.

Fix by clamping index_end to index_text.len(). This is safe because the
computed index text is an optimistic approximation — the real staging
happens at the filesystem level via git add/git reset.

Closes ZED-5R2


Release Notes:

- Fixed a source of panics when staging diff hunks
zed-zippy bot added a commit that referenced this pull request Mar 16, 2026
…51641) (cherry-pick to preview) (#51643)

Cherry-pick of #51641 to preview

----
When the buffer is edited after the diff is computed but before staging,
anchor positions shift while diff_base_byte_range values don't. If the
primary (HEAD) hunk extends past the unstaged (index) hunk, an edit in
the extension region causes the overshoot calculation to produce an
index_end that exceeds index_text.len(), panicking in
rope::Cursor::suffix.

Fix by clamping index_end to index_text.len(). This is safe because the
computed index text is an optimistic approximation — the real staging
happens at the filesystem level via git add/git reset.

Closes ZED-5R2


Release Notes:

- Fixed a source of panics when staging diff hunks

Co-authored-by: Lukas Wirth <lukas@zed.dev>
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 staff Pull requests authored by a current member of Zed staff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants