Skip to content

Commit

Permalink
patch 9.0.0864: crash when using "!!" without a previous shell command
Browse files Browse the repository at this point in the history
Problem:    Crash when using "!!" without a previous shell command.
Solution:   Check "prevcmd" is not NULL. (closes #11487)
  • Loading branch information
brammool committed Nov 12, 2022
1 parent 4c8d2f0 commit 6600447
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/ex_cmds.c
Expand Up @@ -858,6 +858,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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 13 additions & 0 deletions src/testdir/test_shell.vim
Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -695,6 +695,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
864,
/**/
863,
/**/
Expand Down

0 comments on commit 6600447

Please sign in to comment.