From 9b10b4cc6463d901b893ad2d522c629d066607d5 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 20 Apr 2022 10:26:33 +0800 Subject: vim-patch:8.1.1756: autocommand that splits window messes up window layout Problem: Autocommand that splits window messes up window layout. Solution: Disallow splitting a window while closing one. In ":all" give an error when moving a window will not work. https://github.com/vim/vim/commit/1417c766f55e5959b31da488417b7d9b141404af Expected error number was changed to E242 in Vim in patch 8.2.1183, and patch 8.2.2420 (which has already been ported) made the test no longer throw E249 in Vim, so just use E242 in the test. --- src/nvim/buffer.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/nvim/buffer.c') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 4d914acea4..78426568b4 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -4855,6 +4855,10 @@ void do_arg_all(int count, int forceit, int keep_tabs) if (keep_tabs) { new_curwin = wp; new_curtab = curtab; + } else if (wp->w_frame->fr_parent != curwin->w_frame->fr_parent) { + emsg(_("E249: window layout changed unexpectedly")); + i = count; + break; } else { win_move_after(wp, curwin); } -- cgit From 1664e3d4bcc122e6a3b064a3fe20fdc163f6ae9d Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 20 Apr 2022 10:05:02 +0800 Subject: vim-patch:8.2.2476: using freed memory when splitting window while closing buffer Problem: Using freed memory when using an autocommand to split a window while a buffer is being closed. Solution: Disallow splitting when the buffer has b_locked_split set. https://github.com/vim/vim/commit/983d83ff1cd796ff321074335fa53fbe7ac45a46 Put the error message in window.c. Cherry-pick a memory leak fix from Vim patch 8.2.0399. Test still fails. --- src/nvim/buffer.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/nvim/buffer.c') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 78426568b4..628e398fd4 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -466,6 +466,7 @@ bool close_buffer(win_T *win, buf_T *buf, int action, bool abort_if_last, bool i // When the buffer is no longer in a window, trigger BufWinLeave if (buf->b_nwindows == 1) { buf->b_locked++; + buf->b_locked_split++; if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname, false, buf) && !bufref_valid(&bufref)) { // Autocommands deleted the buffer. @@ -473,6 +474,7 @@ bool close_buffer(win_T *win, buf_T *buf, int action, bool abort_if_last, bool i return false; } buf->b_locked--; + buf->b_locked_split--; if (abort_if_last && last_nonfloat(win)) { // Autocommands made this the only window. emsg(_(e_auabort)); @@ -483,6 +485,7 @@ bool close_buffer(win_T *win, buf_T *buf, int action, bool abort_if_last, bool i // BufHidden if (!unload_buf) { buf->b_locked++; + buf->b_locked_split++; if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname, false, buf) && !bufref_valid(&bufref)) { // Autocommands deleted the buffer. @@ -490,6 +493,7 @@ bool close_buffer(win_T *win, buf_T *buf, int action, bool abort_if_last, bool i return false; } buf->b_locked--; + buf->b_locked_split--; if (abort_if_last && last_nonfloat(win)) { // Autocommands made this the only window. emsg(_(e_auabort)); @@ -678,6 +682,7 @@ void buf_freeall(buf_T *buf, int flags) // Make sure the buffer isn't closed by autocommands. buf->b_locked++; + buf->b_locked_split++; bufref_T bufref; set_bufref(&bufref, buf); @@ -703,6 +708,7 @@ void buf_freeall(buf_T *buf, int flags) return; } buf->b_locked--; + buf->b_locked_split--; // If the buffer was in curwin and the window has changed, go back to that // window, if it still exists. This avoids that ":edit x" triggering a @@ -1466,8 +1472,8 @@ void set_curbuf(buf_T *buf, int action) set_bufref(&prevbufref, prevbuf); set_bufref(&newbufref, buf); - // Autocommands may delete the current buffer and/or the buffer we want to go - // to. In those cases don't close the buffer. + // Autocommands may delete the current buffer and/or the buffer we want to + // go to. In those cases don't close the buffer. if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, false, curbuf) || (bufref_valid(&prevbufref) && bufref_valid(&newbufref) && !aborting())) { -- cgit From f531fb97ff5009d2ac279a83da9b9e911c350c89 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 20 Apr 2022 11:11:39 +0800 Subject: vim-patch:8.2.4791: events triggered in different order when reusing buffer Problem: Autocmd events triggered in different order when reusing an empty buffer. Solution: Call buff_freeall() earlier. (Charlie Groves, closes vim/vim#10198) https://github.com/vim/vim/commit/fef4485ef58d5937b170c6dc69431359469fc9cd Test failure becomes very strange. --- src/nvim/buffer.c | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) (limited to 'src/nvim/buffer.c') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 628e398fd4..30bd37fe7f 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -1748,21 +1748,14 @@ buf_T *buflist_new(char_u *ffname_arg, char_u *sfname_arg, linenr_T lnum, int fl buf = curbuf; // It's like this buffer is deleted. Watch out for autocommands that // change curbuf! If that happens, allocate a new buffer anyway. - if (curbuf->b_p_bl) { - apply_autocmds(EVENT_BUFDELETE, NULL, NULL, false, curbuf); - } - if (buf == curbuf) { - apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, false, curbuf); + buf_freeall(buf, BFA_WIPE | BFA_DEL); + if (buf != curbuf) { // autocommands deleted the buffer! + return NULL; } if (aborting()) { // autocmds may abort script processing xfree(ffname); return NULL; } - if (buf == curbuf) { - // Make sure 'bufhidden' and 'buftype' are empty - clear_string_option(&buf->b_p_bh); - clear_string_option(&buf->b_p_bt); - } } if (buf != curbuf || curbuf == NULL) { buf = xcalloc(1, sizeof(buf_T)); @@ -1782,14 +1775,6 @@ buf_T *buflist_new(char_u *ffname_arg, char_u *sfname_arg, linenr_T lnum, int fl buf->b_wininfo = xcalloc(1, sizeof(wininfo_T)); if (buf == curbuf) { - // free all things allocated for this buffer - buf_freeall(buf, 0); - if (buf != curbuf) { // autocommands deleted the buffer! - return NULL; - } - if (aborting()) { // autocmds may abort script processing - return NULL; - } free_buffer_stuff(buf, kBffInitChangedtick); // delete local vars et al. // Init the options. -- cgit