summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2022-11-12 16:36:35 +0000
committerBram Moolenaar <Bram@vim.org>2022-11-12 16:36:35 +0000
commit6600447c7b0a1be3a64d07a318bacdfaae0cac4b (patch)
treed95ddfe17a9666dd6db6fd80e9adb0bdef42a615
parent4c8d2f02b3ce037bbe1d5ee12887e343c6bde88f (diff)
patch 9.0.0864: crash when using "!!" without a previous shell commandv9.0.0864
Problem: Crash when using "!!" without a previous shell command. Solution: Check "prevcmd" is not NULL. (closes #11487)
-rw-r--r--src/ex_cmds.c23
-rw-r--r--src/testdir/test_shell.vim13
-rw-r--r--src/version.c2
3 files changed, 36 insertions, 2 deletions
diff --git a/src/ex_cmds.c b/src/ex_cmds.c
index 3cf07e4825..5b398089fd 100644
--- a/src/ex_cmds.c
+++ b/src/ex_cmds.c
@@ -859,6 +859,21 @@ 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_no_previous_command));
+ 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.
@@ -913,9 +928,8 @@ do_bang(
len += (int)STRLEN(newcmd);
if (ins_prevcmd)
{
- if (prevcmd == NULL)
+ if (!prevcmd_is_set())
{
- emsg(_(e_no_previous_command));
vim_free(newcmd);
return;
}
@@ -971,6 +985,9 @@ do_bang(
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.
@@ -1020,6 +1037,8 @@ do_bang(
do_filter(line1, line2, eap, newcmd, do_in, do_out);
apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
}
+
+theend:
if (free_newcmd)
vim_free(newcmd);
}
diff --git a/src/testdir/test_shell.vim b/src/testdir/test_shell.vim
index 3ac4681e31..7d91dff358 100644
--- a/src/testdir/test_shell.vim
+++ b/src/testdir/test_shell.vim
@@ -282,4 +282,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
diff --git a/src/version.c b/src/version.c
index 64ed6487e7..a0c4371ce6 100644
--- a/src/version.c
+++ b/src/version.c
@@ -696,6 +696,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 864,
+/**/
863,
/**/
862,