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