3ef2ca
To: vim_dev@googlegroups.com
3ef2ca
Subject: Patch 7.4.251
3ef2ca
Fcc: outbox
3ef2ca
From: Bram Moolenaar <Bram@moolenaar.net>
3ef2ca
Mime-Version: 1.0
3ef2ca
Content-Type: text/plain; charset=UTF-8
3ef2ca
Content-Transfer-Encoding: 8bit
3ef2ca
------------
3ef2ca
3ef2ca
Patch 7.4.251
3ef2ca
Problem:    Crash when BufAdd autocommand wipes out the buffer.
3ef2ca
Solution:   Check for buffer to still be valid. Postpone freeing the buffer
3ef2ca
	    structure. (Hirohito Higashi)
3ef2ca
Files:	    src/buffer.c, src/ex_cmds.c, src/fileio.c, src/globals.h
3ef2ca
3ef2ca
3ef2ca
*** ../vim-7.4.250/src/buffer.c	2014-03-23 15:12:29.907264336 +0100
3ef2ca
--- src/buffer.c	2014-04-06 19:55:53.563350929 +0200
3ef2ca
***************
3ef2ca
*** 676,683 ****
3ef2ca
  #endif
3ef2ca
  #ifdef FEAT_AUTOCMD
3ef2ca
      aubuflocal_remove(buf);
3ef2ca
  #endif
3ef2ca
!     vim_free(buf);
3ef2ca
  }
3ef2ca
  
3ef2ca
  /*
3ef2ca
--- 676,691 ----
3ef2ca
  #endif
3ef2ca
  #ifdef FEAT_AUTOCMD
3ef2ca
      aubuflocal_remove(buf);
3ef2ca
+     if (autocmd_busy)
3ef2ca
+     {
3ef2ca
+ 	/* Do not free the buffer structure while autocommands are executing,
3ef2ca
+ 	 * it's still needed. Free it when autocmd_busy is reset. */
3ef2ca
+ 	buf->b_next = au_pending_free_buf;
3ef2ca
+ 	au_pending_free_buf = buf;
3ef2ca
+     }
3ef2ca
+     else
3ef2ca
  #endif
3ef2ca
! 	vim_free(buf);
3ef2ca
  }
3ef2ca
  
3ef2ca
  /*
3ef2ca
***************
3ef2ca
*** 1681,1687 ****
3ef2ca
--- 1689,1699 ----
3ef2ca
  	    buf->b_p_bl = TRUE;
3ef2ca
  #ifdef FEAT_AUTOCMD
3ef2ca
  	    if (!(flags & BLN_DUMMY))
3ef2ca
+ 	    {
3ef2ca
  		apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
3ef2ca
+ 		if (!buf_valid(buf))
3ef2ca
+ 		    return NULL;
3ef2ca
+ 	    }
3ef2ca
  #endif
3ef2ca
  	}
3ef2ca
  	return buf;
3ef2ca
***************
3ef2ca
*** 1857,1864 ****
3ef2ca
--- 1869,1882 ----
3ef2ca
      if (!(flags & BLN_DUMMY))
3ef2ca
      {
3ef2ca
  	apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
3ef2ca
+ 	if (!buf_valid(buf))
3ef2ca
+ 	    return NULL;
3ef2ca
  	if (flags & BLN_LISTED)
3ef2ca
+ 	{
3ef2ca
  	    apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
3ef2ca
+ 	    if (!buf_valid(buf))
3ef2ca
+ 		return NULL;
3ef2ca
+ 	}
3ef2ca
  # ifdef FEAT_EVAL
3ef2ca
  	if (aborting())		/* autocmds may abort script processing */
3ef2ca
  	    return NULL;
3ef2ca
*** ../vim-7.4.250/src/ex_cmds.c	2014-04-04 19:00:46.351940169 +0200
3ef2ca
--- src/ex_cmds.c	2014-04-06 20:41:37.899356924 +0200
3ef2ca
***************
3ef2ca
*** 3343,3348 ****
3ef2ca
--- 3343,3354 ----
3ef2ca
  #endif
3ef2ca
  	    buf = buflist_new(ffname, sfname, 0L,
3ef2ca
  		    BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
3ef2ca
+ #ifdef FEAT_AUTOCMD
3ef2ca
+ 	    /* autocommands may change curwin and curbuf */
3ef2ca
+ 	    if (oldwin != NULL)
3ef2ca
+ 		oldwin = curwin;
3ef2ca
+ 	    old_curbuf = curbuf;
3ef2ca
+ #endif
3ef2ca
  	}
3ef2ca
  	if (buf == NULL)
3ef2ca
  	    goto theend;
3ef2ca
*** ../vim-7.4.250/src/fileio.c	2014-04-02 14:05:33.999887839 +0200
3ef2ca
--- src/fileio.c	2014-04-06 20:34:24.063355976 +0200
3ef2ca
***************
3ef2ca
*** 9548,9560 ****
3ef2ca
  
3ef2ca
      /*
3ef2ca
       * When stopping to execute autocommands, restore the search patterns and
3ef2ca
!      * the redo buffer.
3ef2ca
       */
3ef2ca
      if (!autocmd_busy)
3ef2ca
      {
3ef2ca
  	restore_search_patterns();
3ef2ca
  	restoreRedobuff();
3ef2ca
  	did_filetype = FALSE;
3ef2ca
      }
3ef2ca
  
3ef2ca
      /*
3ef2ca
--- 9548,9566 ----
3ef2ca
  
3ef2ca
      /*
3ef2ca
       * When stopping to execute autocommands, restore the search patterns and
3ef2ca
!      * the redo buffer.  Free buffers in the au_pending_free_buf list.
3ef2ca
       */
3ef2ca
      if (!autocmd_busy)
3ef2ca
      {
3ef2ca
  	restore_search_patterns();
3ef2ca
  	restoreRedobuff();
3ef2ca
  	did_filetype = FALSE;
3ef2ca
+ 	while (au_pending_free_buf != NULL)
3ef2ca
+ 	{
3ef2ca
+ 	    buf_T *b = au_pending_free_buf->b_next;
3ef2ca
+ 	    vim_free(au_pending_free_buf);
3ef2ca
+ 	    au_pending_free_buf = b;
3ef2ca
+ 	}
3ef2ca
      }
3ef2ca
  
3ef2ca
      /*
3ef2ca
*** ../vim-7.4.250/src/globals.h	2014-03-23 15:12:29.943264337 +0100
3ef2ca
--- src/globals.h	2014-04-06 20:32:58.339355789 +0200
3ef2ca
***************
3ef2ca
*** 386,391 ****
3ef2ca
--- 386,396 ----
3ef2ca
  /* When deleting the current buffer, another one must be loaded.  If we know
3ef2ca
   * which one is preferred, au_new_curbuf is set to it */
3ef2ca
  EXTERN buf_T	*au_new_curbuf INIT(= NULL);
3ef2ca
+ 
3ef2ca
+ /* When deleting the buffer and autocmd_busy is TRUE, do not free the buffer
3ef2ca
+  * but link it in the list starting with au_pending_free_buf, using b_next.
3ef2ca
+  * Free the buffer when autocmd_busy is set to FALSE. */
3ef2ca
+ EXTERN buf_T	*au_pending_free_buf INIT(= NULL);
3ef2ca
  #endif
3ef2ca
  
3ef2ca
  #ifdef FEAT_MOUSE
3ef2ca
*** ../vim-7.4.250/src/version.c	2014-04-05 21:59:35.939178415 +0200
3ef2ca
--- src/version.c	2014-04-06 19:52:46.887350521 +0200
3ef2ca
***************
3ef2ca
*** 736,737 ****
3ef2ca
--- 736,739 ----
3ef2ca
  {   /* Add new patch number below this line */
3ef2ca
+ /**/
3ef2ca
+     251,
3ef2ca
  /**/
3ef2ca
3ef2ca
-- 
3ef2ca
hundred-and-one symptoms of being an internet addict:
3ef2ca
37. You start looking for hot HTML addresses in public restrooms.
3ef2ca
3ef2ca
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
3ef2ca
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
3ef2ca
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
3ef2ca
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///