From af3b49bf9c66575da20bbcf4f6e006390e1cf2c7 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Fri, 11 Apr 2025 09:48:28 +0200 Subject: [PATCH] Fix kill-selection crash when selection start is out-of-bounds This part of the code could use some love; when we happen to clear the selected text, we should end the selection. But that's not how it works today. This is fine for Vi mode, because Vi mode never deletes in visual mode. Let's fix the crash for now. Fixes #11367 --- src/reader.rs | 5 ++++- tests/pexpects/bind.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/reader.rs b/src/reader.rs index a63bf578c..27a603ab1 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -4067,8 +4067,11 @@ fn clear_pager(&mut self) { fn get_selection(&self) -> Option> { let selection = self.selection?; - let start = selection.start; + let start = std::cmp::min(selection.start, self.command_line.len()); let end = std::cmp::min(selection.stop, self.command_line.len()); + if start == end { + return None; + } Some(start..end) } diff --git a/tests/pexpects/bind.py b/tests/pexpects/bind.py index 74af4af35..fe287f573 100644 --- a/tests/pexpects/bind.py +++ b/tests/pexpects/bind.py @@ -30,6 +30,21 @@ expect_prompt(increment=False) send("\f") expect_prompt(increment=False) +# Test that kill-selection after selection is cleared doesn't crash +sendline("bind ctrl-space begin-selection") +expect_prompt() +sendline("bind ctrl-w kill-selection end-selection") +expect_prompt() +send("echo 123") +# Send Ctrl-Space using CSI u encoding +send("\x1b[32;5u") +# Send Ctrl-C to clear the command line +send("\x1b[99;5u") +# Send Ctrl-W which used to crash +send("\x1b[119;5u") +sendline("bind --erase ctrl-space ctrl-w") +expect_prompt() + # Fish should start in default-mode (i.e., emacs) bindings. The default escape # timeout is 30ms. #