Patch 8.2.4631
Problem:    Crash when switching window in BufWipeout autocommand.
Solution:   Put any buffer in the window to avoid it being NULL.
            (closes #10024)
Files:      src/window.c, src/buffer.c, src/testdir/test_autocmd.vim


*** ../vim-8.2.4630/src/window.c        2022-03-22 20:42:09.178172849 +0000
--- src/window.c        2022-03-26 16:26:03.063788271 +0000
***************
*** 2285,2290 ****
--- 2285,2325 ----
  }
  #endif
  
+     static void
+ win_init_empty(win_T *wp)
+ {
+     redraw_win_later(wp, NOT_VALID);
+     wp->w_lines_valid = 0;
+     wp->w_cursor.lnum = 1;
+     wp->w_curswant = wp->w_cursor.col = 0;
+     wp->w_cursor.coladd = 0;
+     wp->w_pcmark.lnum = 1;    // pcmark not cleared but set to line 1
+     wp->w_pcmark.col = 0;
+     wp->w_prev_pcmark.lnum = 0;
+     wp->w_prev_pcmark.col = 0;
+     wp->w_topline = 1;
+ #ifdef FEAT_DIFF
+     wp->w_topfill = 0;
+ #endif
+     wp->w_botline = 2;
+ #if defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
+     wp->w_s = &wp->w_buffer->b_s;
+ #endif
+ #ifdef FEAT_TERMINAL
+     term_reset_wincolor(wp);
+ #endif
+ }
+ 
+ /*
+  * Init the current window "curwin".
+  * Called when a new file is being edited.
+  */
+     void
+ curwin_init(void)
+ {
+     win_init_empty(curwin);
+ }
+ 
  /*
   * Close all windows for buffer "buf".
   */
***************
*** 2786,2792 ****
--- 2821,2837 ----
      for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
        ;
      if (ptp == NULL || tp == curtab)
+     {
+       // If the buffer was removed from the window we have to give it any
+       // buffer.
+       if (win_valid_any_tab(win) && win->w_buffer == NULL)
+       {
+           win->w_buffer = firstbuf;
+           ++firstbuf->b_nwindows;
+           win_init_empty(win);
+       }
        return;
+     }
  
      // Autocommands may have closed the window already.
      for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
***************
*** 3685,3725 ****
        emsg(_(e_other_window_contains_changes));
  }
  
-     static void
- win_init_empty(win_T *wp)
- {
-     redraw_win_later(wp, NOT_VALID);
-     wp->w_lines_valid = 0;
-     wp->w_cursor.lnum = 1;
-     wp->w_curswant = wp->w_cursor.col = 0;
-     wp->w_cursor.coladd = 0;
-     wp->w_pcmark.lnum = 1;    // pcmark not cleared but set to line 1
-     wp->w_pcmark.col = 0;
-     wp->w_prev_pcmark.lnum = 0;
-     wp->w_prev_pcmark.col = 0;
-     wp->w_topline = 1;
- #ifdef FEAT_DIFF
-     wp->w_topfill = 0;
- #endif
-     wp->w_botline = 2;
- #if defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
-     wp->w_s = &wp->w_buffer->b_s;
- #endif
- #ifdef FEAT_TERMINAL
-     term_reset_wincolor(wp);
- #endif
- }
- 
- /*
-  * Init the current window "curwin".
-  * Called when a new file is being edited.
-  */
-     void
- curwin_init(void)
- {
-     win_init_empty(curwin);
- }
- 
  /*
   * Allocate the first window and put an empty buffer in it.
   * Called from main().
--- 3730,3735 ----
*** ../vim-8.2.4630/src/buffer.c        2022-03-22 18:12:57.529367476 +0000
--- src/buffer.c        2022-03-26 15:58:06.922072213 +0000
***************
*** 708,713 ****
--- 708,717 ----
       */
      if (wipe_buf)
      {
+       // Do not wipe out the buffer if it is used in a window.
+       if (buf->b_nwindows > 0)
+           return FALSE;
+ 
        if (action == DOBUF_WIPE_REUSE)
        {
            // we can re-use this buffer number, store it
*** ../vim-8.2.4630/src/testdir/test_autocmd.vim        2022-03-22 
18:12:57.529367476 +0000
--- src/testdir/test_autocmd.vim        2022-03-26 16:01:14.369470493 +0000
***************
*** 2990,2993 ****
--- 2990,3010 ----
    bwipe Xb.txt
  endfunc
  
+ func Test_bufwipeout_changes_window()
+   " This should not crash, but we don't have any expectations about what
+   " happens, changing window in BufWipeout has unpredictable results.
+   tabedit
+   let g:window_id = win_getid()
+   topleft new
+   setlocal bufhidden=wipe
+   autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
+   tabprevious
+   +tabclose
+ 
+   unlet g:window_id
+   au! BufWipeout
+   %bwipe!
+ endfunc
+ 
+ 
  " vim: shiftwidth=2 sts=2 expandtab
*** ../vim-8.2.4630/src/version.c       2022-03-26 13:27:06.201576216 +0000
--- src/version.c       2022-03-26 14:48:45.427767865 +0000
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     4631,
  /**/

-- 
Windows
M!uqoms

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20220326163112.2FB701C0C1F%40moolenaar.net.

Raspunde prin e-mail lui