# buffer_diff: Fix panic when staging hunks with stale buffer snapshot \(\#51641\) · gitcafe/zed

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

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

Visibility: public

Requested revision: ebd80d7995710ce7516fb09dc799bcc51578da96

Requested commit: ebd80d7995710ce7516fb09dc799bcc51578da96

Commit: ebd80d7995710ce7516fb09dc799bcc51578da96

Tree: 6d32444b4de3c722d3812236d274072d23e9d7c0

Author: Lukas Wirth

Committer: GitHub

## Message

```
buffer_diff: Fix panic when staging hunks with stale buffer snapshot (#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
```

## Parents

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

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