From 4cb766324b4f32207e49fbeb6b85d45d16ef65ba Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Wed, 14 Feb 2024 10:50:56 +0100 Subject: [PATCH] Fix regression in forward-single-char This crashes if the autosuggesion is exhausted. C++ used autosuggestion.text.substr(pos, 1) which throws if pos is OOB but not if pos + 1 is. --- src/reader.rs | 14 ++++++++------ tests/checks/tmux-complete.fish | 10 ++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index eb76111df..019c5beb0 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -3944,12 +3944,14 @@ fn accept_autosuggestion( self.autosuggestion.text.clone(), ); } else if single { - self.replace_substring( - EditableLineTag::Commandline, - self.command_line.len()..self.command_line.len(), - self.autosuggestion.text[self.command_line.len()..self.command_line.len() + 1] - .to_owned(), - ); + let pos = self.command_line.len(); + if pos + 1 < self.autosuggestion.text.len() { + self.replace_substring( + EditableLineTag::Commandline, + pos..pos, + self.autosuggestion.text[pos..pos + 1].to_owned(), + ); + } } else { // Accept characters according to the specified style. let mut state = MoveWordStateMachine::new(style); diff --git a/tests/checks/tmux-complete.fish b/tests/checks/tmux-complete.fish index f4c060a87..ed22c12fc 100644 --- a/tests/checks/tmux-complete.fish +++ b/tests/checks/tmux-complete.fish @@ -84,3 +84,13 @@ isolated-tmux send-keys 'echo sug' C-w C-z tmux-sleep isolated-tmux capture-pane -p # CHECK: prompt 6> echo suggest this + +isolated-tmux send-keys C-u 'bind \cs forward-single-char' Enter C-l +isolated-tmux send-keys 'echo suggest thi' +tmux-sleep +isolated-tmux send-keys C-s +tmux-sleep +isolated-tmux send-keys C-s +tmux-sleep +isolated-tmux capture-pane -p +# CHECK: prompt 7> echo suggest this