# Attempt to log error instead of crash in bracket highlighting \(\#32837\) · gitcafe/zed

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

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

Visibility: public

Requested revision: d92d52b508c11092ca51a1174be14aabcc2fe5f2

Requested commit: d92d52b508c11092ca51a1174be14aabcc2fe5f2

Commit: d92d52b508c11092ca51a1174be14aabcc2fe5f2

Tree: fc1469b6301de27513d918f820154a80e35bf913

Author: Michael Sloan

Committer: GitHub

## Message

````
Attempt to log error instead of crash in bracket highlighting (#32837)

Crashes look like:

```
Panic `offset 632 is greater than the snapshot.len() 631` on thread 0 (com.apple.main-thread)

<multi_buffer::MultiBufferSnapshot>::innermost_enclosing_bracket_ranges::<usize>
editor::highlight_matching_bracket::refresh_matching_bracket_highlights
<gpui::app::App>::update_window_id::<bool, <gpui::app::context::Context<editor::Editor>>::subscribe_in<multi_buffer::MultiBuffer, multi_buffer::Event, <editor::Editor>::on_buffer_event>::{closure#0}::{closure#0}>::{closure#0}
<gpui::app::context::Context<editor::Editor>>::subscribe_in::<multi_buffer::MultiBuffer, multi_buffer::Event, <editor::Editor>::on_buffer_event>::{closure#0}
<gpui::app::App>::flush_effects
<project::lsp_store::LocalLspStore>::format_buffer_locally::{closure#0}
<project::lsp_store::LspStore>::format::{closure#1}::{closure#0}::<i32>
```

Though `format_buffer_locally` is not always present. Both issue reports
mention usage of the agent. I suspect this is somehow a result of agent
format-on-save combined with the user's cursor being at the end of the
buffer as it's getting edited by the agent.

The offsets are always off-by-one in the error, so at first I thought
the issue was the condition `head < snapshot.buffer_snapshot.len()`
before setting `tail` to be `head + 1`, but an offset equal to len is
valid. Seems like to get a `to_offset` crash, `head` must be greater
than `len`. Which is quite weird, a selection's offset should never be
out of bounds.

Since this code is just about highlighting brackets, this PR logs an
error instead of crashing in the `head > len` case.

Closes #32732, #32171

Release Notes:

- N/A
````

## Parents

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

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