From ae3532e9eccda2e3203c929ed256b0a1c7e3dc31 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Fri, 14 Mar 2025 16:21:50 +0100 Subject: [PATCH] screen: Fix crash if prompt contains backspace fish_wcwidth_visible can return -1, so usize::try_from fails. Fixes #11280 (cherry picked from commit c03de2086ac46ad9a79928407395e49d3dadb723) --- src/screen.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/screen.rs b/src/screen.rs index ad6f43b2b..158512e1e 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -1636,7 +1636,11 @@ fn measure_run_from( width = next_tab_stop(width); } else { // Ordinary char. Add its width with care to ignore control chars which have width -1. - width += usize::try_from(fish_wcwidth_visible(input.char_at(idx))).unwrap(); + if let Ok(ww) = usize::try_from(fish_wcwidth_visible(input.char_at(idx))) { + width += ww; + } else { + width = width.saturating_sub(1); + } } idx += 1; } @@ -1682,7 +1686,8 @@ fn truncate_run( curr_width = measure_run_from(run, 0, None, cache); idx = 0; } else { - let char_width = usize::try_from(fish_wcwidth_visible(c)).unwrap(); + // FIXME: In case of backspace, this would remove the last width. + let char_width = usize::try_from(fish_wcwidth_visible(c)).unwrap_or(0); curr_width -= std::cmp::min(curr_width, char_width); run.remove(idx); }