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