Karsten Hopp 81c285
To: vim-dev@vim.org
Karsten Hopp 81c285
Subject: Patch 7.2.188
Karsten Hopp 81c285
Fcc: outbox
Karsten Hopp 81c285
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp 81c285
Mime-Version: 1.0
Karsten Hopp 81c285
Content-Type: text/plain; charset=UTF-8
Karsten Hopp 81c285
Content-Transfer-Encoding: 8bit
Karsten Hopp 81c285
------------
Karsten Hopp 81c285
Karsten Hopp 81c285
Patch 7.2.188
Karsten Hopp 81c285
Problem:    Crash with specific use of function calls. (Meikel Brandmeyer)
Karsten Hopp 81c285
Solution:   Make sure the items referenced by a function call are not freed
Karsten Hopp 81c285
	    twice.  (based on patch from Nico Weber)
Karsten Hopp 81c285
Files:	    src/eval.c
Karsten Hopp 81c285
Karsten Hopp 81c285
Karsten Hopp 81c285
*** ../vim-7.2.187/src/eval.c	2009-05-16 17:29:37.000000000 +0200
Karsten Hopp 81c285
--- src/eval.c	2009-05-22 20:04:22.000000000 +0200
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 129,136 ****
Karsten Hopp 81c285
--- 129,139 ----
Karsten Hopp 81c285
  /*
Karsten Hopp 81c285
   * When recursively copying lists and dicts we need to remember which ones we
Karsten Hopp 81c285
   * have done to avoid endless recursiveness.  This unique ID is used for that.
Karsten Hopp 81c285
+  * The last bit is used for previous_funccal, ignored when comparing.
Karsten Hopp 81c285
   */
Karsten Hopp 81c285
  static int current_copyID = 0;
Karsten Hopp 81c285
+ #define COPYID_INC 2
Karsten Hopp 81c285
+ #define COPYID_MASK (~0x1)
Karsten Hopp 81c285
  
Karsten Hopp 81c285
  /*
Karsten Hopp 81c285
   * Array to hold the hashtab with variables local to each sourced script.
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 439,444 ****
Karsten Hopp 81c285
--- 442,448 ----
Karsten Hopp 81c285
  static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Karsten Hopp 81c285
  static char_u *list2string __ARGS((typval_T *tv, int copyID));
Karsten Hopp 81c285
  static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Karsten Hopp 81c285
+ static int free_unref_items __ARGS((int copyID));
Karsten Hopp 81c285
  static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
Karsten Hopp 81c285
  static void set_ref_in_list __ARGS((list_T *l, int copyID));
Karsten Hopp 81c285
  static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 6494,6507 ****
Karsten Hopp 81c285
      int
Karsten Hopp 81c285
  garbage_collect()
Karsten Hopp 81c285
  {
Karsten Hopp 81c285
!     dict_T	*dd;
Karsten Hopp 81c285
!     list_T	*ll;
Karsten Hopp 81c285
!     int		copyID = ++current_copyID;
Karsten Hopp 81c285
      buf_T	*buf;
Karsten Hopp 81c285
      win_T	*wp;
Karsten Hopp 81c285
      int		i;
Karsten Hopp 81c285
      funccall_T	*fc, **pfc;
Karsten Hopp 81c285
!     int		did_free = FALSE;
Karsten Hopp 81c285
  #ifdef FEAT_WINDOWS
Karsten Hopp 81c285
      tabpage_T	*tp;
Karsten Hopp 81c285
  #endif
Karsten Hopp 81c285
--- 6498,6510 ----
Karsten Hopp 81c285
      int
Karsten Hopp 81c285
  garbage_collect()
Karsten Hopp 81c285
  {
Karsten Hopp 81c285
!     int		copyID;
Karsten Hopp 81c285
      buf_T	*buf;
Karsten Hopp 81c285
      win_T	*wp;
Karsten Hopp 81c285
      int		i;
Karsten Hopp 81c285
      funccall_T	*fc, **pfc;
Karsten Hopp 81c285
!     int		did_free;
Karsten Hopp 81c285
!     int		did_free_funccal = FALSE;
Karsten Hopp 81c285
  #ifdef FEAT_WINDOWS
Karsten Hopp 81c285
      tabpage_T	*tp;
Karsten Hopp 81c285
  #endif
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 6511,6520 ****
Karsten Hopp 81c285
--- 6514,6538 ----
Karsten Hopp 81c285
      may_garbage_collect = FALSE;
Karsten Hopp 81c285
      garbage_collect_at_exit = FALSE;
Karsten Hopp 81c285
  
Karsten Hopp 81c285
+     /* We advance by two because we add one for items referenced through
Karsten Hopp 81c285
+      * previous_funccal. */
Karsten Hopp 81c285
+     current_copyID += COPYID_INC;
Karsten Hopp 81c285
+     copyID = current_copyID;
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
      /*
Karsten Hopp 81c285
       * 1. Go through all accessible variables and mark all lists and dicts
Karsten Hopp 81c285
       *    with copyID.
Karsten Hopp 81c285
       */
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
+     /* Don't free variables in the previous_funccal list unless they are only
Karsten Hopp 81c285
+      * referenced through previous_funccal.  This must be first, because if
Karsten Hopp 81c285
+      * the item is referenced elsewhere it must not be freed. */
Karsten Hopp 81c285
+     for (fc = previous_funccal; fc != NULL; fc = fc->caller)
Karsten Hopp 81c285
+     {
Karsten Hopp 81c285
+ 	set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
Karsten Hopp 81c285
+ 	set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
Karsten Hopp 81c285
+     }
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
      /* script-local variables */
Karsten Hopp 81c285
      for (i = 1; i <= ga_scripts.ga_len; ++i)
Karsten Hopp 81c285
  	set_ref_in_ht(&SCRIPT_VARS(i), copyID);
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 6546,6556 ****
Karsten Hopp 81c285
      /* v: vars */
Karsten Hopp 81c285
      set_ref_in_ht(&vimvarht, copyID);
Karsten Hopp 81c285
  
Karsten Hopp 81c285
      /*
Karsten Hopp 81c285
!      * 2. Go through the list of dicts and free items without the copyID.
Karsten Hopp 81c285
       */
Karsten Hopp 81c285
      for (dd = first_dict; dd != NULL; )
Karsten Hopp 81c285
! 	if (dd->dv_copyID != copyID)
Karsten Hopp 81c285
  	{
Karsten Hopp 81c285
  	    /* Free the Dictionary and ordinary items it contains, but don't
Karsten Hopp 81c285
  	     * recurse into Lists and Dictionaries, they will be in the list
Karsten Hopp 81c285
--- 6564,6610 ----
Karsten Hopp 81c285
      /* v: vars */
Karsten Hopp 81c285
      set_ref_in_ht(&vimvarht, copyID);
Karsten Hopp 81c285
  
Karsten Hopp 81c285
+     /* Free lists and dictionaries that are not referenced. */
Karsten Hopp 81c285
+     did_free = free_unref_items(copyID);
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
+     /* check if any funccal can be freed now */
Karsten Hopp 81c285
+     for (pfc = &previous_funccal; *pfc != NULL; )
Karsten Hopp 81c285
+     {
Karsten Hopp 81c285
+ 	if (can_free_funccal(*pfc, copyID))
Karsten Hopp 81c285
+ 	{
Karsten Hopp 81c285
+ 	    fc = *pfc;
Karsten Hopp 81c285
+ 	    *pfc = fc->caller;
Karsten Hopp 81c285
+ 	    free_funccal(fc, TRUE);
Karsten Hopp 81c285
+ 	    did_free = TRUE;
Karsten Hopp 81c285
+ 	    did_free_funccal = TRUE;
Karsten Hopp 81c285
+ 	}
Karsten Hopp 81c285
+ 	else
Karsten Hopp 81c285
+ 	    pfc = &(*pfc)->caller;
Karsten Hopp 81c285
+     }
Karsten Hopp 81c285
+     if (did_free_funccal)
Karsten Hopp 81c285
+ 	/* When a funccal was freed some more items might be garbage
Karsten Hopp 81c285
+ 	 * collected, so run again. */
Karsten Hopp 81c285
+ 	(void)garbage_collect();
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
+     return did_free;
Karsten Hopp 81c285
+ }
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
+ /*
Karsten Hopp 81c285
+  * Free lists and dictionaries that are no longer referenced.
Karsten Hopp 81c285
+  */
Karsten Hopp 81c285
+     static int
Karsten Hopp 81c285
+ free_unref_items(copyID)
Karsten Hopp 81c285
+     int copyID;
Karsten Hopp 81c285
+ {
Karsten Hopp 81c285
+     dict_T	*dd;
Karsten Hopp 81c285
+     list_T	*ll;
Karsten Hopp 81c285
+     int		did_free = FALSE;
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
      /*
Karsten Hopp 81c285
!      * Go through the list of dicts and free items without the copyID.
Karsten Hopp 81c285
       */
Karsten Hopp 81c285
      for (dd = first_dict; dd != NULL; )
Karsten Hopp 81c285
! 	if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Karsten Hopp 81c285
  	{
Karsten Hopp 81c285
  	    /* Free the Dictionary and ordinary items it contains, but don't
Karsten Hopp 81c285
  	     * recurse into Lists and Dictionaries, they will be in the list
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 6565,6576 ****
Karsten Hopp 81c285
  	    dd = dd->dv_used_next;
Karsten Hopp 81c285
  
Karsten Hopp 81c285
      /*
Karsten Hopp 81c285
!      * 3. Go through the list of lists and free items without the copyID.
Karsten Hopp 81c285
!      *    But don't free a list that has a watcher (used in a for loop), these
Karsten Hopp 81c285
!      *    are not referenced anywhere.
Karsten Hopp 81c285
       */
Karsten Hopp 81c285
      for (ll = first_list; ll != NULL; )
Karsten Hopp 81c285
! 	if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Karsten Hopp 81c285
  	{
Karsten Hopp 81c285
  	    /* Free the List and ordinary items it contains, but don't recurse
Karsten Hopp 81c285
  	     * into Lists and Dictionaries, they will be in the list of dicts
Karsten Hopp 81c285
--- 6619,6631 ----
Karsten Hopp 81c285
  	    dd = dd->dv_used_next;
Karsten Hopp 81c285
  
Karsten Hopp 81c285
      /*
Karsten Hopp 81c285
!      * Go through the list of lists and free items without the copyID.
Karsten Hopp 81c285
!      * But don't free a list that has a watcher (used in a for loop), these
Karsten Hopp 81c285
!      * are not referenced anywhere.
Karsten Hopp 81c285
       */
Karsten Hopp 81c285
      for (ll = first_list; ll != NULL; )
Karsten Hopp 81c285
! 	if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
Karsten Hopp 81c285
! 						      && ll->lv_watch == NULL)
Karsten Hopp 81c285
  	{
Karsten Hopp 81c285
  	    /* Free the List and ordinary items it contains, but don't recurse
Karsten Hopp 81c285
  	     * into Lists and Dictionaries, they will be in the list of dicts
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 6584,6603 ****
Karsten Hopp 81c285
  	else
Karsten Hopp 81c285
  	    ll = ll->lv_used_next;
Karsten Hopp 81c285
  
Karsten Hopp 81c285
-     /* check if any funccal can be freed now */
Karsten Hopp 81c285
-     for (pfc = &previous_funccal; *pfc != NULL; )
Karsten Hopp 81c285
-     {
Karsten Hopp 81c285
- 	if (can_free_funccal(*pfc, copyID))
Karsten Hopp 81c285
- 	{
Karsten Hopp 81c285
- 	    fc = *pfc;
Karsten Hopp 81c285
- 	    *pfc = fc->caller;
Karsten Hopp 81c285
- 	    free_funccal(fc, TRUE);
Karsten Hopp 81c285
- 	    did_free = TRUE;
Karsten Hopp 81c285
- 	}
Karsten Hopp 81c285
- 	else
Karsten Hopp 81c285
- 	    pfc = &(*pfc)->caller;
Karsten Hopp 81c285
-     }
Karsten Hopp 81c285
- 
Karsten Hopp 81c285
      return did_free;
Karsten Hopp 81c285
  }
Karsten Hopp 81c285
  
Karsten Hopp 81c285
--- 6639,6644 ----
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 18842,18847 ****
Karsten Hopp 81c285
--- 18883,18889 ----
Karsten Hopp 81c285
  {
Karsten Hopp 81c285
      hash_init(&dict->dv_hashtab);
Karsten Hopp 81c285
      dict->dv_refcount = DO_NOT_FREE_CNT;
Karsten Hopp 81c285
+     dict->dv_copyID = 0;
Karsten Hopp 81c285
      dict_var->di_tv.vval.v_dict = dict;
Karsten Hopp 81c285
      dict_var->di_tv.v_type = VAR_DICT;
Karsten Hopp 81c285
      dict_var->di_tv.v_lock = VAR_FIXED;
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 21294,21301 ****
Karsten Hopp 81c285
      current_funccal = fc->caller;
Karsten Hopp 81c285
      --depth;
Karsten Hopp 81c285
  
Karsten Hopp 81c285
!     /* if the a:000 list and the a: dict are not referenced we can free the
Karsten Hopp 81c285
!      * funccall_T and what's in it. */
Karsten Hopp 81c285
      if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
Karsten Hopp 81c285
  	    && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
Karsten Hopp 81c285
  	    && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
Karsten Hopp 81c285
--- 21336,21343 ----
Karsten Hopp 81c285
      current_funccal = fc->caller;
Karsten Hopp 81c285
      --depth;
Karsten Hopp 81c285
  
Karsten Hopp 81c285
!     /* If the a:000 list and the l: and a: dicts are not referenced we can
Karsten Hopp 81c285
!      * free the funccall_T and what's in it. */
Karsten Hopp 81c285
      if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
Karsten Hopp 81c285
  	    && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
Karsten Hopp 81c285
  	    && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 21334,21340 ****
Karsten Hopp 81c285
  
Karsten Hopp 81c285
  /*
Karsten Hopp 81c285
   * Return TRUE if items in "fc" do not have "copyID".  That means they are not
Karsten Hopp 81c285
!  * referenced from anywhere.
Karsten Hopp 81c285
   */
Karsten Hopp 81c285
      static int
Karsten Hopp 81c285
  can_free_funccal(fc, copyID)
Karsten Hopp 81c285
--- 21376,21382 ----
Karsten Hopp 81c285
  
Karsten Hopp 81c285
  /*
Karsten Hopp 81c285
   * Return TRUE if items in "fc" do not have "copyID".  That means they are not
Karsten Hopp 81c285
!  * referenced from anywhere that is in use.
Karsten Hopp 81c285
   */
Karsten Hopp 81c285
      static int
Karsten Hopp 81c285
  can_free_funccal(fc, copyID)
Karsten Hopp 81c285
*** ../vim-7.2.187/src/version.c	2009-05-23 14:27:43.000000000 +0200
Karsten Hopp 81c285
--- src/version.c	2009-05-24 13:20:49.000000000 +0200
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 678,679 ****
Karsten Hopp 81c285
--- 678,681 ----
Karsten Hopp 81c285
  {   /* Add new patch number below this line */
Karsten Hopp 81c285
+ /**/
Karsten Hopp 81c285
+     188,
Karsten Hopp 81c285
  /**/
Karsten Hopp 81c285
Karsten Hopp 81c285
-- 
Karsten Hopp 81c285
ARTHUR:    ... and I am your king ....
Karsten Hopp 81c285
OLD WOMAN: Ooooh!  I didn't know we had a king.  I thought we were an
Karsten Hopp 81c285
           autonomous collective ...
Karsten Hopp 81c285
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
Karsten Hopp 81c285
Karsten Hopp 81c285
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp 81c285
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp 81c285
\\\        download, build and distribute -- http://www.A-A-P.org        ///
Karsten Hopp 81c285
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///