Skip to content

Crash when typing multibyte chars in global search box #13297

@shnarazk

Description

@shnarazk

Summary

Some string manipulations don't handle multibyte characters correctly.
The following seems to fix the issue. I'm not sure if this is an appropriate approach.

diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs
index 3c97a93c..94f9f775 100644
--- a/helix-term/src/ui/prompt.rs
+++ b/helix-term/src/ui/prompt.rs
@@ -551,6 +551,9 @@ pub fn render_prompt(&mut self, area: Rect, surface: &mut Surface, cx: &mut Cont
             if self.truncate_end && self.cursor - self.anchor >= self.line_area.width as usize {
                 self.anchor += 1;
             }
+            while !self.line.is_char_boundary(self.anchor) {
+                self.anchor += 1;
+            }
 
             surface.set_string_anchored(
                 self.line_area.x,
@@ -734,7 +737,10 @@ fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind)
             .clip_left(self.prompt.len() as u16)
             .clip_right(if self.prompt.is_empty() { 2 } else { 0 });
 
-        let anchor = self.anchor.min(self.line.len().saturating_sub(1));
+        let mut anchor = self.anchor.min(self.line.len().saturating_sub(1));
+        while !self.line.is_char_boundary(anchor) {
+            anchor += 1;
+        }
         let mut col = area.left() as usize
             + UnicodeWidthStr::width(&self.line[anchor..self.cursor.max(anchor)]);
 

The second part fixes the crash by moving cursor after insertion.

Reproduction Steps

I tried this:

  1. RUST_BACKTRACE=1 hx
  2. space /
  3. type マルチバイトキャラクター列 in or paste it from system clipboard

then, this happened:

thread 'main' panicked at helix-term/src/ui/prompt.rs:560:36:
byte index 8 is not a char boundary; it is inside 'チ' (bytes 6..9) of `マルチバイトキャラク
ター列
`
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::str::slice_error_fail_rt
   3: core::str::slice_error_fail
   4: helix_term::ui::prompt::Prompt::render_prompt
   5: <helix_term::ui::picker::Picker<I,D> as helix_term::compositor::Component>::render
   6: helix_term::application::Application::render::{{closure}}
   7: hx::main_impl::{{closure}}
   8: tokio::runtime::park::CachedParkThread::block_on
   9: tokio::runtime::context::runtime::enter_runtime
  10: tokio::runtime::runtime::Runtime::block_on
  11: hx::main

Helix log

No response

Platform

macOS

Terminal Emulator

alacritty 0.15.1

Installation Method

source

Helix Version

helix 25.01.1 (29789f2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions