From 7bb0dd08dbcd8cfeeaea725e2c00e9e5cfcae2bd Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Fri, 11 Aug 2023 10:09:51 +0100 Subject: vim-patch:9.1.0048: Abort opening cmdwin if autocmds screw things up Problem: Autocmds triggered from opening the cmdwin (in win_split and do_ecmd) can cause issues such as E199, as the current checks are insufficient. Solution: Commands executed from the cmdwin apply to the old curwin/buf, so they should be kept in a "suspended" state; abort if they've changed. Also abort if cmdwin/buf was tampered with, and check that curwin is correct. Try to clean up the cmdwin buffer (only if hidden and non-current to simplify things; the same approach is used when closing cmdwin normally), and add a beep. (Sean Dewar) Rename the old Test_cmdwin_interrupted() like in the patch (can be moved to test_cmdwin.vim when v9.0.0027 is ported). Move the error message to `e_active_window_or_buffer_changed_or_deleted`. https://github.com/vim/vim/commit/43b395ec2e7d24a067d7cb00109818b64da144a5 --- src/nvim/ex_getln.c | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index f49652fc92..9b56fffeda 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -219,6 +219,9 @@ static int cedit_key = -1; ///< key value of 'cedit' option static handle_T cmdpreview_bufnr = 0; static int cmdpreview_ns = 0; +static const char e_active_window_or_buffer_changed_or_deleted[] + = N_("E199: Active window or buffer changed or deleted"); + static void save_viewstate(win_T *wp, viewstate_T *vs) FUNC_ATTR_NONNULL_ALL { @@ -4323,6 +4326,14 @@ static int open_cmdwin(void) ga_clear(&winsizes); return K_IGNORE; } + // win_split() autocommands may have messed with the old window or buffer. + // Treat it as abandoning this command-line. + if (!win_valid(old_curwin) || curwin == old_curwin || !bufref_valid(&old_curbuf) + || old_curwin->w_buffer != old_curbuf.br_buf) { + beep_flush(); + ga_clear(&winsizes); + return Ctrl_C; + } // Don't let quitting the More prompt make this fail. got_int = false; @@ -4332,14 +4343,29 @@ static int open_cmdwin(void) cmdwin_win = curwin; cmdwin_old_curwin = old_curwin; - // Create empty command-line buffer. - if (buf_open_scratch(0, _("[Command Line]")) == FAIL) { - // Some autocommand messed it up? - win_close(curwin, true, false); - ga_clear(&winsizes); + // Create empty command-line buffer. Be especially cautious of BufLeave + // autocommands from do_ecmd(), as cmdwin restrictions do not apply to them! + const int newbuf_status = buf_open_scratch(0, _("[Command Line]")); + const bool cmdwin_valid = win_valid(cmdwin_win); + if (newbuf_status == FAIL || !cmdwin_valid || curwin != cmdwin_win || !win_valid(old_curwin) + || !bufref_valid(&old_curbuf) || old_curwin->w_buffer != old_curbuf.br_buf) { + if (newbuf_status == OK) { + set_bufref(&bufref, curbuf); + } + if (cmdwin_valid && !last_window(cmdwin_win)) { + win_close(cmdwin_win, true, false); + } + // win_close() autocommands may have already deleted the buffer. + if (newbuf_status == OK && bufref_valid(&bufref) && bufref.br_buf != curbuf) { + close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, false, false); + } + cmdwin_type = 0; + cmdwin_level = 0; cmdwin_win = NULL; cmdwin_old_curwin = NULL; + beep_flush(); + ga_clear(&winsizes); return Ctrl_C; } cmdwin_buf = curbuf; @@ -4444,11 +4470,12 @@ static int open_cmdwin(void) exmode_active = save_exmode; - // Safety check: The old window or buffer was deleted: It's a bug when - // this happens! - if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) { + // Safety check: The old window or buffer was changed or deleted: It's a bug + // when this happens! + if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf) + || old_curwin->w_buffer != old_curbuf.br_buf) { cmdwin_result = Ctrl_C; - emsg(_("E199: Active window or buffer deleted")); + emsg(_(e_active_window_or_buffer_changed_or_deleted)); } else { win_T *wp; // autocmds may abort script processing -- cgit