From c0f10d3fe018990b68cfa47bd84693449100f449 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 18 Apr 2023 14:09:29 +0800 Subject: vim-patch:9.0.0783: ":!" doesn't do anything but does update the previous command Problem: ":!" doesn't do anything but does update the previous command. Solution: Do not have ":!" change the previous command. (Martin Tournoij, closes vim/vim#11372) https://github.com/vim/vim/commit/8107a2a8af80a53a61734b600539c5beb4782991 Co-authored-by: Bram Moolenaar --- runtime/doc/various.txt | 1 + src/nvim/ex_cmds.c | 6 ++++++ test/old/testdir/test_shell.vim | 31 +++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/runtime/doc/various.txt b/runtime/doc/various.txt index 7b0e724e31..84449cdae6 100644 --- a/runtime/doc/various.txt +++ b/runtime/doc/various.txt @@ -248,6 +248,7 @@ g8 Print the hex values of the bytes used in the < *:!cmd* *:!* :!{cmd} Execute {cmd} with 'shell'. See also |:terminal|. + `:!` without a {cmd} is a no-op, it does nothing. The command runs in a non-interactive shell connected to a pipe (not a terminal). Use |:terminal| to run an diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index a1b43113a6..cc8c4aeca8 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -1012,6 +1012,12 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out } } while (trailarg != NULL); + // Don't do anything if there is no command as there isn't really anything + // useful in running "sh -c ''". Avoids changing "prevcmd". + if (strlen(newcmd) == 0) { + return; + } + xfree(prevcmd); prevcmd = newcmd; diff --git a/test/old/testdir/test_shell.vim b/test/old/testdir/test_shell.vim index 3d0056bfc1..b65752dbf2 100644 --- a/test/old/testdir/test_shell.vim +++ b/test/old/testdir/test_shell.vim @@ -206,4 +206,35 @@ func Test_set_shell() call delete('Xtestout') endfunc +func Test_shell_repeat() + CheckUnix + + let save_shell = &shell + + call writefile(['#!/bin/sh', 'echo "Cmd: [$*]" > Xlog'], 'Xtestshell', 'D') + call setfperm('Xtestshell', "r-x------") + set shell=./Xtestshell + defer delete('Xlog') + + call feedkeys(":!echo coconut\", 'xt') " Run command + call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog')) + + call feedkeys(":!!\", 'xt') " Re-run previous + call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog')) + + call writefile(['empty'], 'Xlog') + call feedkeys(":!\", 'xt') " :! is a no-op + call assert_equal(['empty'], readfile('Xlog')) + + call feedkeys(":!!\", 'xt') " :! doesn't clear previous command + call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog')) + + call feedkeys(":!echo banana\", 'xt') " Make sure setting previous command keeps working after a :! no-op + call assert_equal(['Cmd: [-c echo banana]'], readfile('Xlog')) + call feedkeys(":!!\", 'xt') + call assert_equal(['Cmd: [-c echo banana]'], readfile('Xlog')) + + let &shell = save_shell +endfunc + " vim: shiftwidth=2 sts=2 expandtab -- cgit From 5b77dde8dd281b1db1f503d9b270c3697f88bd65 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 18 Apr 2023 14:22:16 +0800 Subject: vim-patch:9.0.0785: memory leak with empty shell command Problem: Memory leak with empty shell command. Solution: Free the allocated memory when bailing out. https://github.com/vim/vim/commit/9652249a2d02318a28a63a7b5711f25652e8f969 Co-authored-by: Bram Moolenaar --- src/nvim/ex_cmds.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index cc8c4aeca8..015f651872 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -1015,6 +1015,7 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out // Don't do anything if there is no command as there isn't really anything // useful in running "sh -c ''". Avoids changing "prevcmd". if (strlen(newcmd) == 0) { + xfree(newcmd); return; } -- cgit From 187ba3efce4acc197ce6cc9559a905eec97bacbe Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 18 Apr 2023 14:23:09 +0800 Subject: vim-patch:9.0.0815 https://github.com/vim/vim/commit/9c50eeb40117413bf59a9da904c8d0921ed0a6e6 Co-authored-by: Martin Tournoij --- src/nvim/ex_cmds.c | 14 ++++++-------- test/old/testdir/test_shell.vim | 4 ++-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 015f651872..f7447cda1b 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -988,6 +988,8 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out } if (ins_prevcmd) { STRCAT(t, prevcmd); + } else { + xfree(t); } p = t + strlen(t); STRCAT(t, trailarg); @@ -1012,16 +1014,12 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out } } while (trailarg != NULL); - // Don't do anything if there is no command as there isn't really anything - // useful in running "sh -c ''". Avoids changing "prevcmd". - if (strlen(newcmd) == 0) { - xfree(newcmd); - return; + // Don't clear "prevcmd" if there is no command to run. + if (strlen(newcmd) > 0) { + xfree(prevcmd); + prevcmd = newcmd; } - xfree(prevcmd); - prevcmd = newcmd; - if (bangredo) { // put cmd in redo buffer for ! command // If % or # appears in the command, it must have been escaped. // Reescape them, so that redoing them does not substitute them by the diff --git a/test/old/testdir/test_shell.vim b/test/old/testdir/test_shell.vim index b65752dbf2..828fd1fa40 100644 --- a/test/old/testdir/test_shell.vim +++ b/test/old/testdir/test_shell.vim @@ -223,8 +223,8 @@ func Test_shell_repeat() call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog')) call writefile(['empty'], 'Xlog') - call feedkeys(":!\", 'xt') " :! is a no-op - call assert_equal(['empty'], readfile('Xlog')) + call feedkeys(":!\", 'xt') " :! + call assert_equal(['Cmd: [-c ]'], readfile('Xlog')) call feedkeys(":!!\", 'xt') " :! doesn't clear previous command call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog')) -- cgit From a1e0f6c07fc575dfac4cd4c0d8c395b8d3f1a4f4 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 18 Apr 2023 14:28:00 +0800 Subject: vim-patch:9.0.0817 https://github.com/vim/vim/commit/fb0cf2357e0c85bbfd9f9178705ad8d77b6b3b4e Co-authored-by: Bram Moolenaar --- src/nvim/ex_cmds.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index f7447cda1b..db9cf94cba 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -988,8 +988,6 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out } if (ins_prevcmd) { STRCAT(t, prevcmd); - } else { - xfree(t); } p = t + strlen(t); STRCAT(t, trailarg); -- cgit From 2a94dcf0c592464222c87f5606799ca51a0b9c07 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 18 Apr 2023 14:30:08 +0800 Subject: vim-patch:9.0.0820: memory leak with empty shell command Problem: Memory leak with empty shell command. Solution: Free the empty string. https://github.com/vim/vim/commit/03d6e6f42b0deeb02d52c8a48c14abe431370c1c Co-authored-by: Bram Moolenaar --- src/nvim/ex_cmds.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index db9cf94cba..55dbe17ad6 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -1012,10 +1012,13 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out } } while (trailarg != NULL); - // Don't clear "prevcmd" if there is no command to run. + // Only set "prevcmd" if there is a command to run, otherwise keep te one + // we have. if (strlen(newcmd) > 0) { xfree(prevcmd); prevcmd = newcmd; + } else { + free_newcmd = true; } if (bangredo) { // put cmd in redo buffer for ! command @@ -1031,6 +1034,9 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out } // Add quotes around the command, for shells that need them. if (*p_shq != NUL) { + if (free_newcmd) { + xfree(newcmd); + } newcmd = xmalloc(strlen(prevcmd) + 2 * strlen(p_shq) + 1); STRCPY(newcmd, p_shq); STRCAT(newcmd, prevcmd); -- cgit From 9180c18c462a4945657899b732189da6f2ea2eaf Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 18 Apr 2023 14:31:40 +0800 Subject: 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 --- src/nvim/eval/userfunc.c | 2 +- src/nvim/ex_cmds.c | 20 ++++++++++++++++++-- test/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 index e138c50b6a..63d5f94f11 100644 --- 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 index 55dbe17ad6..b75ff45843 100644 --- 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 index 828fd1fa40..7172d5ba33 100644 --- 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 !!\" + 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 -- cgit From 2e21d1d5ddb7a7fb5978bdce2812417026b276a9 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 18 Apr 2023 14:36:45 +0800 Subject: vim-patch:6ebe4f970b8b Update runtime files https://github.com/vim/vim/commit/6ebe4f970b8b398087076a72a7aae6e680fb92da Co-authored-by: Bram Moolenaar --- runtime/doc/various.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/runtime/doc/various.txt b/runtime/doc/various.txt index 84449cdae6..7b0e724e31 100644 --- a/runtime/doc/various.txt +++ b/runtime/doc/various.txt @@ -248,7 +248,6 @@ g8 Print the hex values of the bytes used in the < *:!cmd* *:!* :!{cmd} Execute {cmd} with 'shell'. See also |:terminal|. - `:!` without a {cmd} is a no-op, it does nothing. The command runs in a non-interactive shell connected to a pipe (not a terminal). Use |:terminal| to run an -- cgit