neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

commit 9180c18c462a4945657899b732189da6f2ea2eaf
parent 2a94dcf0c592464222c87f5606799ca51a0b9c07
Author: zeertzjq <zeertzjq@outlook.com>
Date:   Tue, 18 Apr 2023 14:31:40 +0800

vim-patch:9.0.0864: crash when using "!!" without a previous shell command

Problem:    Crash when using "!!" without a previous shell command.
Solution:   Check "prevcmd" is not NULL. (closes vim/vim#11487)

https://github.com/vim/vim/commit/6600447c7b0a1be3a64d07a318bacdfaae0cac4b

Co-authored-by: Bram Moolenaar <Bram@vim.org>

Diffstat:
Msrc/nvim/eval/userfunc.c | 2+-
Msrc/nvim/ex_cmds.c | 20++++++++++++++++++--
Mtest/old/testdir/test_shell.vim | 13+++++++++++++
3 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/src/nvim/eval/userfunc.c b/src/nvim/eval/userfunc.c @@ -3207,7 +3207,7 @@ static int ex_defer_inner(char *name, char **arg, const partial_T *const partial } /// Return true if currently inside a function call. -/// Give an error message and return FALSE when not. +/// Give an error message and return false when not. bool can_add_defer(void) { if (get_current_funccal() == NULL) { diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c @@ -933,6 +933,17 @@ void free_prev_shellcmd(void) #endif +/// Check that "prevcmd" is not NULL. If it is NULL then give an error message +/// and return false. +static int prevcmd_is_set(void) +{ + if (prevcmd == NULL) { + emsg(_(e_noprev)); + return false; + } + return true; +} + /// Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd" /// Bangs in the argument are replaced with the previously entered command. /// Remember the argument. @@ -974,8 +985,7 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out len += strlen(newcmd); } if (ins_prevcmd) { - if (prevcmd == NULL) { - emsg(_(e_noprev)); + if (!prevcmd_is_set()) { xfree(newcmd); return; } @@ -1022,6 +1032,10 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out } if (bangredo) { // put cmd in redo buffer for ! command + if (!prevcmd_is_set()) { + goto theend; + } + // If % or # appears in the command, it must have been escaped. // Reescape them, so that redoing them does not substitute them by the // buffername. @@ -1059,6 +1073,8 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out do_filter(line1, line2, eap, newcmd, do_in, do_out); apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, false, curbuf); } + +theend: if (free_newcmd) { xfree(newcmd); } diff --git a/test/old/testdir/test_shell.vim b/test/old/testdir/test_shell.vim @@ -237,4 +237,17 @@ func Test_shell_repeat() let &shell = save_shell endfunc +func Test_shell_no_prevcmd() + " this doesn't do anything, just check it doesn't crash + let after =<< trim END + exe "normal !!\<CR>" + call writefile([v:errmsg, 'done'], 'Xtestdone') + qall! + END + if RunVim([], after, '--clean') + call assert_equal(['E34: No previous command', 'done'], readfile('Xtestdone')) + endif + call delete('Xtestdone') +endfunc + " vim: shiftwidth=2 sts=2 expandtab