From 19fed6bde176586b43b5b9ff37146ddf2f5a65ce Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 16 Dec 2023 21:30:08 +0800 Subject: [PATCH] vim-patch:9.0.2168: Moving tabpages on :drop may cause an endless loop (#26605) Problem: Moving tabpages on :drop may cause an endless loop Solution: Disallow moving tabpages on :drop when cleaning up the arglist first Moving tabpages during drop command may cause an endless loop When executing a :tab drop command, Vim will close all windows not in the argument list. This triggers various autocommands. If a user has created an 'au Tabenter * :tabmove -' autocommand, this can cause Vim to end up in an endless loop, when trying to iterate over all tabs (which would trigger the tabmove autocommand, which will change the tpnext pointer, etc). So instead of blocking all autocommands before we actually try to edit the given file, lets simply disallow to move tabpages around. Otherwise, we may change the expected number of events triggered during a :drop command, which users may rely on (there is actually a test, that expects various TabLeave/TabEnter autocommands) and would therefore be a backwards incompatible change. Don't make this an error, as this could trigger several times during the drop command, but silently ignore the :tabmove command in this case (and it should in fact finally trigger successfully when loading the given file in a new tab). So let's just be quiet here instead. fixes: vim/vim#13676 closes: vim/vim#13686 https://github.com/vim/vim/commit/df12e39b8b9dd39056e22b452276622cb7b617fd Co-authored-by: Christian Brabandt --- src/nvim/arglist.c | 4 ++++ src/nvim/autocmd.h | 4 ++-- src/nvim/window.c | 4 ++++ src/nvim/window.h | 2 ++ test/old/testdir/test_tabpage.vim | 19 +++++++++++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/nvim/arglist.c b/src/nvim/arglist.c index 541534abf9..064543b430 100644 --- a/src/nvim/arglist.c +++ b/src/nvim/arglist.c @@ -851,6 +851,9 @@ static void arg_all_close_unused_windows(arg_all_state_T *aall) if (aall->had_tab > 0) { goto_tabpage_tp(first_tabpage, true, true); } + + // moving tabpages around in an autocommand may cause an endless loop + tabpage_move_disallowed++; while (true) { win_T *wpnext = NULL; tabpage_T *tpnext = curtab->tp_next; @@ -950,6 +953,7 @@ static void arg_all_close_unused_windows(arg_all_state_T *aall) } goto_tabpage_tp(tpnext, true, true); } + tabpage_move_disallowed--; } /// Open up to "count" windows for the files in the argument list "aall->alist". diff --git a/src/nvim/autocmd.h b/src/nvim/autocmd.h index ea8f32feb2..b0a5a13026 100644 --- a/src/nvim/autocmd.h +++ b/src/nvim/autocmd.h @@ -28,8 +28,8 @@ EXTERN win_T *last_cursormoved_win INIT( = NULL); EXTERN pos_T last_cursormoved INIT( = { 0, 0, 0 }); EXTERN bool autocmd_busy INIT( = false); ///< Is apply_autocmds() busy? -EXTERN int autocmd_no_enter INIT( = false); ///< *Enter autocmds disabled -EXTERN int autocmd_no_leave INIT( = false); ///< *Leave autocmds disabled +EXTERN int autocmd_no_enter INIT( = false); ///< Buf/WinEnter autocmds disabled +EXTERN int autocmd_no_leave INIT( = false); ///< Buf/WinLeave autocmds disabled EXTERN bool did_filetype INIT( = false); ///< FileType event found /// value for did_filetype when starting to execute autocommands EXTERN bool keep_filetype INIT( = false); diff --git a/src/nvim/window.c b/src/nvim/window.c index 618a3f760e..0a09d8e27d 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -4426,6 +4426,10 @@ void tabpage_move(int nr) return; } + if (tabpage_move_disallowed) { + return; + } + int n = 1; tabpage_T *tp; diff --git a/src/nvim/window.h b/src/nvim/window.h index d70292508a..3c88965324 100644 --- a/src/nvim/window.h +++ b/src/nvim/window.h @@ -43,6 +43,8 @@ enum { LOWEST_WIN_ID = 1000, }; +EXTERN int tabpage_move_disallowed INIT( = 0); ///< moving tabpages around disallowed + /// Set to true if 'cmdheight' was explicitly set to 0. EXTERN bool p_ch_was_zero INIT( = false); diff --git a/test/old/testdir/test_tabpage.vim b/test/old/testdir/test_tabpage.vim index 4e7b09b9e7..d3393af04b 100644 --- a/test/old/testdir/test_tabpage.vim +++ b/test/old/testdir/test_tabpage.vim @@ -905,4 +905,23 @@ func Test_tabpage_last_line() bwipe! endfunc +" this was causing an endless loop +func Test_tabpage_drop_tabmove() + augroup TestTabpageTabmove + au! + autocmd! TabEnter * :if tabpagenr() > 1 | tabmove - | endif + augroup end + $tab drop XTab_99.log + $tab drop XTab_98.log + $tab drop XTab_97.log + + autocmd! TestTabpageTabmove + augroup! TestTabpageTabmove + + " clean up + bwipe! + bwipe! + bwipe! +endfunc + " vim: shiftwidth=2 sts=2 expandtab