Blame SOURCES/0001-patch-8.2.4281-using-freed-memory-with-lopen-and-bwi.patch

da4393
diff -up vim82/src/buffer.c.cve0443 vim82/src/buffer.c
da4393
--- vim82/src/buffer.c.cve0443	2021-03-22 10:02:42.000000000 +0100
da4393
+++ vim82/src/buffer.c	2022-02-10 08:33:19.159488384 +0100
da4393
@@ -1710,6 +1710,7 @@ set_curbuf(buf_T *buf, int action)
da4393
 #endif
da4393
     bufref_T	newbufref;
da4393
     bufref_T	prevbufref;
da4393
+    int		valid;
da4393
 
da4393
     setpcmark();
da4393
     if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
da4393
@@ -1763,13 +1764,19 @@ set_curbuf(buf_T *buf, int action)
da4393
     // An autocommand may have deleted "buf", already entered it (e.g., when
da4393
     // it did ":bunload") or aborted the script processing.
da4393
     // If curwin->w_buffer is null, enter_buffer() will make it valid again
da4393
-    if ((buf_valid(buf) && buf != curbuf
da4393
+    valid = buf_valid(buf);
da4393
+    if ((valid && buf != curbuf
da4393
 #ifdef FEAT_EVAL
da4393
 		&& !aborting()
da4393
 #endif
da4393
 	) || curwin->w_buffer == NULL)
da4393
     {
da4393
-	enter_buffer(buf);
da4393
+	// If the buffer is not valid but curwin->w_buffer is NULL we must
da4393
+	// enter some buffer.  Using the last one is hopefully OK.
da4393
+	if (!valid)
da4393
+	    enter_buffer(lastbuf);
da4393
+	else
da4393
+	    enter_buffer(buf);
da4393
 #ifdef FEAT_SYN_HL
da4393
 	if (old_tw != curbuf->b_p_tw)
da4393
 	    check_colorcolumn(curwin);
da4393
@@ -2286,8 +2293,7 @@ free_buf_options(
da4393
     clear_string_option(&buf->b_p_vsts);
da4393
     vim_free(buf->b_p_vsts_nopaste);
da4393
     buf->b_p_vsts_nopaste = NULL;
da4393
-    vim_free(buf->b_p_vsts_array);
da4393
-    buf->b_p_vsts_array = NULL;
da4393
+    VIM_CLEAR(buf->b_p_vsts_array);
da4393
     clear_string_option(&buf->b_p_vts);
da4393
     VIM_CLEAR(buf->b_p_vts_array);
da4393
 #endif
da4393
diff -up vim82/src/testdir/test_quickfix.vim.cve0443 vim82/src/testdir/test_quickfix.vim
da4393
--- vim82/src/testdir/test_quickfix.vim.cve0443	2021-03-22 10:02:42.000000000 +0100
da4393
+++ vim82/src/testdir/test_quickfix.vim	2022-02-10 08:34:10.288204457 +0100
da4393
@@ -923,6 +923,7 @@ func Test_locationlist_curwin_was_closed
da4393
   call assert_fails('lrewind', 'E924:')
da4393
 
da4393
   augroup! testgroup
da4393
+  delfunc R
da4393
 endfunc
da4393
 
da4393
 func Test_locationlist_cross_tab_jump()
da4393
@@ -5372,4 +5373,20 @@ func Test_vimgrep_noswapfile()
da4393
   set swapfile
da4393
 endfunc
da4393
 
da4393
+" Weird sequence of commands that caused entering a wiped-out buffer
da4393
+func Test_lopen_bwipe()
da4393
+  func R()
da4393
+    silent! tab lopen
da4393
+    e x
da4393
+    silent! lfile
da4393
+  endfunc
da4393
+
da4393
+  cal R()
da4393
+  cal R()
da4393
+  cal R()
da4393
+  bw!
da4393
+  delfunc R
da4393
+endfunc
da4393
+
da4393
+
da4393
 " vim: shiftwidth=2 sts=2 expandtab