Repository: alacritty/alacritty (language: rust)

Issue:
Crash during leaving search with selection
I'm not sure what exactly triggered it, but that's the crash log

```
thread 'main' panicked at 'index out of bounds: the len is 98 but the index is 18446744073709551517', /build/source/alacritty_terminal/src/term/mod.rs:520:26
stack backtrace:
   0:     0x5617d54ecc1c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::had616a77c932ab1b
   1:     0x5617d53e824f - core::fmt::write::h3a35c0754863b3d5
   2:     0x5617d54f9344 - std::io::Write::write_fmt::h502c0f36b9ea0da9
   3:     0x5617d54ec9ef - std::sys_common::backtrace::print::h90cd8adaf174c598
   4:     0x5617d54f4c3e - std::panicking::default_hook::{{closure}}::ha88f96a3c80996f9
   5:     0x5617d54f57b0 - std::panicking::rust_panic_with_hook::h69ad028564cd3ed4
   6:     0x5617d54ecfa4 - std::panicking::begin_panic_handler::{{closure}}::h8ea911da5e199679
   7:     0x5617d54ecf06 - std::sys_common::backtrace::__rust_end_short_backtrace::h02258dadc83c0b77
   8:     0x5617d54f5041 - rust_begin_unwind
   9:     0x5617d5166582 - core::panicking::panic_fmt::h55a49ce1199dc412
  10:     0x5617d5166601 - core::panicking::panic_bounds_check::hbf70d6589eba51c8
  11:     0x5617d528d73a - alacritty_terminal::term::Term<T>::line_to_string::heb368209b2097e38
  12:     0x5617d528ce26 - alacritty_terminal::term::Term<T>::bounds_to_string::hc781b058923da519
                               at /build/source/alacritty_terminal/src/term/mod.rs:505:21
  13:     0x5617d51b8166 - alacritty_terminal::term::Term<T>::selection_to_string::hf41d2387efc67733
                               at /build/source/alacritty_terminal/src/term/mod.rs:490:23
  14:     0x5617d51b8166 - <alacritty::event::ActionContext<N,T> as alacritty::input::ActionContext<T>>::copy_selection::h1112ccab04af04fc
                               at /build/source/alacritty/src/event.rs:261:26
  15:     0x5617d526f023 - <alacritty::event::ActionContext<N,T> as alacritty::input::ActionContext<T>>::cancel_search::hce471099bca60375
                               at /build/source/alacritty/src/event.rs:519:13
  16:     0x5617d526853d - alacritty::input::Processor<T,A>::process_key_bindings::h35dee97d4410461b
                               at /build/source/alacritty/src/input.rs:1061:17
  17:     0x5617d526853d - alacritty::input::Processor<T,A>::key_input::hb6e2dcf26cb54f96
                               at /build/source/alacritty/src/input.rs:994:12
  18:     0x5617d530b354 - alacritty::event::<impl alacritty::input::Processor<alacritty::event::EventProxy,alacritty::event::ActionContext<alacritty_terminal::event_loop::Notifier,alacritty::event::EventProxy>>>::handle_event::h3a3d59cef5c5e6a3
                               at /build/source/alacritty/src/event.rs:1296:25
  19:     0x5617d530b354 - alacritty::window_context::WindowContext::handle_event::h31a66680305ae0b3
                               at /build/source/alacritty/src/window_context.rs:446:13
  20:     0x5617d51c1c73 - alacritty::event::Processor::run::{{closure}}::h24d2b508b1ac29c2
                               at /build/source/alacritty/src/event.rs:1569:25
  21:     0x5617d51c02eb - winit::platform_impl::platform::sticky_exit_callback::hf3630e6b04146970
                               at /build/alacritty-0.12.1-vendor.tar.gz/winit/src/platform_impl/linux/mod.rs:922:9
  22:     0x5617d51c02eb - winit::platform_impl::platform::wayland::event_loop::EventLoop<T>::run_return::h8c8136dad4575858
                               at /build/alacritty-0.12.1-vendor.tar.gz/winit/src/platform_impl/linux/wayland/event_loop/mod.rs:445:13
  23:     0x5617d51c02eb - winit::platform_impl::platform::EventLoop<T>::run_return::h45a89db0a4317078
                               at /build/alacritty-0.12.1-vendor.tar.gz/winit/src/platform_impl/linux/mod.rs:823:56
  24:     0x5617d51c02eb - <winit::event_loop::EventLoop<T> as winit::platform::run_return::EventLoopExtRunReturn>::run_return::h0183dababb7f126f
                               at /build/alacritty-0.12.1-vendor.tar.gz/winit/src/platform/run_return.rs:51:9
  25:     0x5617d51c02eb - alacritty::event::Processor::run::hb9bf9b1eef4447df
                               at /build/source/alacritty/src/event.rs:1499:25
  26:     0x5617d52c4510 - alacritty::alacritty::h8270abd7f24bf144
                               at /build/source/alacritty/src/main.rs:190:18
  27:     0x5617d52c4510 - alacritty::main::h549f1f9b76f7c9d1
                               at /build/source/alacritty/src/main.rs:85:17
  28:     0x5617d5186983 - core::ops::function::FnOnce::call_once::h4581bc79690d0497
                               at /build/rustc-1.70.0-src/library/core/src/ops/function.rs:250:5
  29:     0x5617d5186983 - std::sys_common::backtrace::__rust_begin_short_backtrace::h91c273b99f071235
                               at /build/rustc-1.70.0-src/library/std/src/sys_common/backtrace.rs:134:18
  30:     0x5617d520f8ae - std::rt::lang_start::{{closure}}::hf59c610ce2612480
                               at /build/rustc-1.70.0-src/library/std/src/rt.rs:166:18
  31:     0x5617d52be376 - main
  32:     0x7f7f31de7ace - __libc_start_call_main
  33:     0x7f7f31de7b89 - __libc_start_main@@GLIBC_2.34
  34:     0x5617d5179a95 - _start
  35:                0x0 - <unknown>

```


Discussion / hints:
Hm, I'm not sure how to repro that, and it's weird that we've end up in such state, but, so I guess I'll just add debugging and wait until it happens again and then try to craft a test case out of output.

I think I've seen it outside `cancel_search` as well at least once, but obviously I have only report for that sort of crash as of now.
Ok, that has a bit more context.

```
[7889.436591520s] [DEBUG] [alacritty] Loading "monospace" font
[7889.445284548s] [DEBUG] [crossfont] Loaded Face Face { ft_face: Font Face: Extended, load_flags: NO_BITMAP | TARGET_LIGHT, render_mode: "Lcd", lcd_filter: 1 }
[7889.447804448s] [DEBUG] [crossfont] Loaded Face Face { ft_face: Font Face: Bold Extended, load_flags: NO_BITMAP | TARGET_LIGHT, render_mode: "Lcd", lcd_filter: 1 }
[7889.450234881s] [DEBUG] [crossfont] Loaded Face Face { ft_face: Font Face: Extended Italic, load_flags: NO_BITMAP | TARGET_LIGHT, render_mode: "Lcd", lcd_filter: 1 }
[7889.452691644s] [DEBUG] [crossfont] Loaded Face Face { ft_face: Font Face: Bold Extended Italic, load_flags: NO_BITMAP | TARGET_LIGHT, render_mode: "Lcd", lcd_filter: 1 }
[7889.454696948s] [INFO ] [alacritty] Running on AMD Radeon Graphics (renoir, LLVM 15.0.7, DRM 3.49, 6.1.37)
[7889.454726142s] [INFO ] [alacritty] OpenGL version 4.6 (Core Profile) Mesa 23.1.3, shader_version 4.60
[7889.454734104s] [INFO ] [alacritty] Using OpenGL 3.3 renderer
[7889.455592183s] [DEBUG] [alacritty] Enabled debug logging for OpenGL
[7889.455880561s] [DEBUG] [alacritty] Filling glyph cache with common glyphs
[7889.460441175s] [INFO ] [alacritty] Cell size: 8 x 18
[7889.460462757s] [INFO ] [alacritty] Padding: 3 x 1
[7889.460469322s] [INFO ] [alacritty] Width: 800, Height: 600
[7889.460499843s] [INFO ] [alacritty] PTY dimensions: 33 x 99
[7889.466385011s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7889.471726809s] [DEBUG] [alacritty_terminal] New num_cols is 117 and num_lines is 20
[7889.471843724s] [INFO ] [alacritty] Padding: 3 x 1
[7889.471853642s] [INFO ] [alacritty] Width: 942, Height: 365
[7889.776708499s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7889.995174986s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7889.995579930s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7889.996368866s] [DEBUG] [alacritty_terminal] New num_cols is 117 and num_lines is 41
[7889.998554292s] [INFO ] [alacritty] Padding: 3 x 1
[7889.998596337s] [INFO ] [alacritty] Width: 942, Height: 744
[7896.298860221s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7896.698308201s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7897.224676179s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7898.646799963s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7899.291400915s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7899.292710244s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7912.325824176s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7912.327006252s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7944.092057676s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7944.093284381s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7944.544020635s] [DEBUG] [alacritty_terminal] New num_cols is 117 and num_lines is 28
[7944.544418944s] [INFO ] [alacritty] Padding: 3 x 1
[7944.544459103s] [INFO ] [alacritty] Width: 942, Height: 508
[7945.666900214s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7949.227916440s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7949.696117450s] [DEBUG] [alacritty_terminal] Term::resize dimensions unchanged
[7949.972464759s] [DEBUG] [alacritty_terminal] New num_cols is 239 and num_lines is 59
[7949.975876681s] [INFO ] [alacritty] Padding: 3 x 1
[7949.975935977s] [INFO ] [alacritty] Width: 1920, Height: 1080
[7950.687071317s] [DEBUG] [alacritty_terminal] New num_cols is 239 and num_lines is 58
[7950.687522007s] [INFO ] [alacritty] Padding: 3 x 1
[7950.687553436s] [INFO ] [alacritty] Width: 1920, Height: 1080
[7962.372627904s] [DEBUG] [alacritty_terminal] New num_cols is 117 and num_lines is 27
[7962.372930180s] [INFO ] [alacritty] Padding: 3 x 1
[7962.373011756s] [INFO ] [alacritty] Width: 942, Height: 508
[7962.906179642s] [DEBUG] [alacritty] focused_match: Point { line: Line(43), column: Column(58) }..=Point { line: Line(43), column: Column(60) }
[7962.906262126s] [DEBUG] [alacritty] size_info: SizeInfo { width: 942.0, height: 508.0, cell_width: 8.0, cell_height: 18.0, padding_x: 3.0, padding_y: 1.0, screen_lines: 27, columns: 117 }
[7962.906291739s] [DEBUG] [alacritty] selection: Some(Selection { ty: Simple, region: Anchor { point: Point { line: Line(43), column: Column(58) }, side: Left }..Anchor { point: Point { line: Line(26), column: Column(60) }, side: Right } })
thread 'main' panicked at 'index out of bounds: the len is 27 but the index is 18446744073709551588', /build/source/alacritty_terminal/src/term/mod.rs:520:26
stack backtrace:
   0:     0x55f092485a6c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::had616a77c932ab1b
   1:     0x55f09238116f - core::fmt::write::h3a35c0754863b3d5
   2:     0x55f092492194 - std::io::Write::write_fmt::h502c0f36b9ea0da9
   3:     0x55f09248583f - std::sys_common::backtrace::print::h90cd8adaf174c598
   4:     0x55f09248da8e - std::panicking::default_hook::{{closure}}::ha88f96a3c80996f9
   5:     0x55f09248e600 - std::panicking::rust_panic_with_hook::h69ad028564cd3ed4
   6:     0x55f092485df4 - std::panicking::begin_panic_handler::{{closure}}::h8ea911da5e199679
   7:     0x55f092485d56 - std::sys_common::backtrace::__rust_end_short_backtrace::h02258dadc83c0b77
   8:     0x55f09248de91 - rust_begin_unwind
   9:     0x55f0920fb582 - core::panicking::panic_fmt::h55a49ce1199dc412
  10:     0x55f0920fb601 - core::panicking::panic_bounds_check::hbf70d6589eba51c8
  11:     0x55f092220a5a - alacritty_terminal::term::Term<T>::line_to_string::heb368209b2097e38
  12:     0x55f092220146 - alacritty_terminal::term::Term<T>::bounds_to_string::hc781b058923da519
                               at /build/source/alacritty_terminal/src/term/mod.rs:505:21
  13:     0x55f09214d2a6 - alacritty_terminal::term::Term<T>::selection_to_string::hf41d2387efc67733
                               at /build/source/alacritty_terminal/src/term/mod.rs:490:23
  14:     0x55f09214d2a6 - <alacritty::event::ActionContext<N,T> as alacritty::input::ActionContext<T>>::copy_selection::h1112ccab04af04fc
                               at /build/source/alacritty/src/event.rs:261:26
  15:     0x55f092201aa7 - <alacritty::event::ActionContext<N,T> as alacritty::input::ActionContext<T>>::cancel_search::hce471099bca60375
                               at /build/source/alacritty/src/event.rs:522:13
  16:     0x55f0921fb53d - alacritty::input::Processor<T,A>::process_key_bindings::h35dee97d4410461b
                               at /build/source/alacritty/src/input.rs:1061:17
  17:     0x55f0921fb53d - alacritty::input::Processor<T,A>::key_input::hb6e2dcf26cb54f96
                               at /build/source/alacritty/src/input.rs:994:12
  18:     0x55f0922ae3f4 - alacritty::event::<impl alacritty::input::Processor<alacritty::event::EventProxy,alacritty::event::ActionContext<alacritty_terminal::event_loop::Notifier,alacritty::event::EventProxy>>>::handle_event::h3a3d59cef5c5e6a3
                               at /build/source/alacritty/src/event.rs:1299:25
  19:     0x55f0922ae3f4 - alacritty::window_context::WindowContext::handle_event::h31a66680305ae0b3
                               at /build/source/alacritty/src/window_context.rs:446:13
  20:     0x55f092156e13 - alacritty::event::Processor::run::{{closure}}::h24d2b508b1ac29c2
                               at /build/source/alacritty/src/event.rs:1572:25
  21:     0x55f09215548b - winit::platform_impl::platform::sticky_exit_callback::hf3630e6b04146970
                               at /build/alacritty-0.12.1-vendor.tar.gz/winit/src/platform_impl/linux/mod.rs:922:9
  22:     0x55f09215548b - winit::platform_impl::platform::wayland::event_loop::EventLoop<T>::run_return::h8c8136dad4575858
                               at /build/alacritty-0.12.1-vendor.tar.gz/winit/src/platform_impl/linux/wayland/event_loop/mod.rs:445:13
  23:     0x55f09215548b - winit::platform_impl::platform::EventLoop<T>::run_return::h45a89db0a4317078
                               at /build/alacritty-0.12.1-vendor.tar.gz/winit/src/platform_impl/linux/mod.rs:823:56
  24:     0x55f09215548b - <winit::event_loop::EventLoop<T> as winit::platform::run_return::EventLoopExtRunReturn>::run_return::h0183dababb7f126f
                               at /build/alacritty-0.12.1-vendor.tar.gz/winit/src/platform/run_return.rs:51:9
  25:     0x55f09215548b - alacritty::event::Processor::run::hb9bf9b1eef4447df
                               at /build/source/alacritty/src/event.rs:1502:25
  26:     0x55f092252cd0 - alacritty::alacritty::h8270abd7f24bf144
                               at /build/source/alacritty/src/main.rs:190:18
  27:     0x55f092252cd0 - alacritty::main::h549f1f9b76f7c9d1
                               at /build/source/alacritty/src/main.rs:85:17
  28:     0x55f09211b9d3 - core::ops::function::FnOnce::call_once::h4581bc79690d0497
                               at /build/rustc-1.70.0-src/library/core/src/ops/function.rs:250:5
  29:     0x55f09211b9d3 - std::sys_common::backtrace::__rust_begin_short_backtrace::h91c273b99f071235
                               at /build/rustc-1.70.0-src/library/std/src/sys_common/backtrace.rs:134:18
  30:     0x55f0921a625e - std::rt::lang_start::{{closure}}::hf59c610ce2612480
                               at /build/rustc-1.70.0-src/library/std/src/rt.rs:166:18
  31:     0x55f09224cb36 - main
  32:     0x7fba2ae13ace - __libc_start_call_main
  33:     0x7fba2ae13b89 - __libc_start_main@@GLIBC_2.34
  34:     0x55f09210ea95 - _start
  35:                0x0 - <unknown>

```
That's the patch I've had applied

```diff
diff --git a/alacritty/src/event.rs b/alacritty/src/event.rs
index 52222080..7754b265 100644
--- a/alacritty/src/event.rs
+++ b/alacritty/src/event.rs
@@ -511,11 +511,14 @@ impl<'a, N: Notify + 'a, T: EventListener> input::ActionContext<T> for ActionCon
             // Recover pre-search state in vi mode.
             self.search_reset_state();
         } else if let Some(focused_match) = &self.search_state.focused_match {
+            debug!("focused_match: {:?}", focused_match);
             // Create a selection for the focused match.
             let start = *focused_match.start();
             let end = *focused_match.end();
             self.start_selection(SelectionType::Simple, start, Side::Left);
             self.update_selection(end, Side::Right);
+            debug!("size_info: {:?}", self.size_info());
+            debug!("selection: {:?}", self.terminal.selection);
             self.copy_selection(ClipboardType::Selection);
         }
 

```

https://github.com/alacritty/alacritty/assets/8886672/64085460-b957-47f4-8d63-aef158933e37



Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 80abac434f5..837794735b8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -64,6 +64,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - `Command` modifier persisting after `Cmd + Tab` on macOS
 - Crash on exit when using NVIDIA binary drivers on Wayland
 - `window.startup_mode` applied to window again when creating new tab
+- Crash when leaving search after resize
 
 ### Removed
 
diff --git a/alacritty/src/display/mod.rs b/alacritty/src/display/mod.rs
index 7380e8c3cf8..dfe2809f02e 100644
--- a/alacritty/src/display/mod.rs
+++ b/alacritty/src/display/mod.rs
@@ -598,7 +598,7 @@ impl Display {
         terminal: &mut Term<T>,
         pty_resize_handle: &mut dyn OnResize,
         message_buffer: &MessageBuffer,
-        search_active: bool,
+        search_state: &mut SearchState,
         config: &UiConfig,
     ) where
         T: EventListener,
@@ -643,6 +643,7 @@ impl Display {
         );
 
         // Update number of column/lines in the viewport.
+        let search_active = search_state.history_index.is_some();
         let message_bar_lines = message_buffer.message().map_or(0, |m| m.text(&new_size).len());
         let search_lines = usize::from(search_active);
         new_size.reserve_lines(message_bar_lines + search_lines);
@@ -658,10 +659,14 @@ impl Display {
         // Resize terminal.
         terminal.resize(new_size);
 
-        // Queue renderer update if terminal dimensions/padding changed.
+        // Check if dimensions have changed.
         if new_size != self.size_info {
+            // Queue renderer update.
             let renderer_update = self.pending_renderer_update.get_or_insert(Default::default());
             renderer_update.resize = true;
+
+            // Clear focused search match.
+            search_state.clear_focused_match();
         }
         self.size_info = new_size;
     }
diff --git a/alacritty/src/event.rs b/alacritty/src/event.rs
index 281bcace0b9..beab7c745af 100644
--- a/alacritty/src/event.rs
+++ b/alacritty/src/event.rs
@@ -154,6 +154,11 @@ impl SearchState {
         self.focused_match.as_ref()
     }
 
+    /// Clear the focused match.
+    pub fn clear_focused_match(&mut self) {
+        self.focused_match = None;
+    }
+
     /// Active search dfas.
     pub fn dfas(&mut self) -> Option<&mut RegexSearch> {
         self.dfas.as_mut()
diff --git a/alacritty/src/window_context.rs b/alacritty/src/window_context.rs
index a80aecca4ab..d2f7ffd4019 100644
--- a/alacritty/src/window_context.rs
+++ b/alacritty/src/window_context.rs
@@ -480,7 +480,7 @@ impl WindowContext {
                 &mut self.display,
                 &mut self.notifier,
                 &self.message_buffer,
-                &self.search_state,
+                &mut self.search_state,
                 old_is_searching,
                 &self.config,
             );
@@ -546,7 +546,7 @@ impl WindowContext {
         display: &mut Display,
         notifier: &mut Notifier,
         message_buffer: &MessageBuffer,
-        search_state: &SearchState,
+        search_state: &mut SearchState,
         old_is_searching: bool,
         config: &UiConfig,
     ) {
@@ -559,13 +559,7 @@ impl WindowContext {
             search_state.direction == Direction::Left
         };
 
-        display.handle_update(
-            terminal,
-            notifier,
-            message_buffer,
-            search_state.history_index.is_some(),
-            config,
-        );
+        display.handle_update(terminal, notifier, message_buffer, search_state, config);
 
         let new_is_searching = search_state.history_index.is_some();
         if !old_is_searching && new_is_searching {


Test patch: