634b724
To: vim_dev@googlegroups.com
634b724
Subject: Patch 7.3.055
634b724
Fcc: outbox
634b724
From: Bram Moolenaar <Bram@moolenaar.net>
634b724
Mime-Version: 1.0
634b724
Content-Type: text/plain; charset=UTF-8
634b724
Content-Transfer-Encoding: 8bit
634b724
------------
634b724
634b724
Patch 7.3.055
634b724
Problem:    Recursively nested lists and dictionaries cause a near-endless
634b724
	    loop when comparing them with a copy. (ZyX)
634b724
Solution:   Limit recursiveness in a way that non-recursive structures can
634b724
	    still be nested very deep.
634b724
Files:	    src/eval.c, src/testdir/test55.in, src/testdir/test55.ok
634b724
634b724
634b724
*** ../vim-7.3.054/src/eval.c	2010-10-20 21:22:17.000000000 +0200
634b724
--- src/eval.c	2010-11-10 20:02:57.000000000 +0100
634b724
***************
634b724
*** 434,442 ****
634b724
  static void listitem_free __ARGS((listitem_T *item));
634b724
  static void listitem_remove __ARGS((list_T *l, listitem_T *item));
634b724
  static long list_len __ARGS((list_T *l));
634b724
! static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
634b724
! static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
634b724
! static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
634b724
  static listitem_T *list_find __ARGS((list_T *l, long n));
634b724
  static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
634b724
  static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
634b724
--- 434,442 ----
634b724
  static void listitem_free __ARGS((listitem_T *item));
634b724
  static void listitem_remove __ARGS((list_T *l, listitem_T *item));
634b724
  static long list_len __ARGS((list_T *l));
634b724
! static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
634b724
! static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
634b724
! static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
634b724
  static listitem_T *list_find __ARGS((list_T *l, long n));
634b724
  static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
634b724
  static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
634b724
***************
634b724
*** 4350,4356 ****
634b724
  		else
634b724
  		{
634b724
  		    /* Compare two Lists for being equal or unequal. */
634b724
! 		    n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
634b724
  		    if (type == TYPE_NEQUAL)
634b724
  			n1 = !n1;
634b724
  		}
634b724
--- 4350,4357 ----
634b724
  		else
634b724
  		{
634b724
  		    /* Compare two Lists for being equal or unequal. */
634b724
! 		    n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
634b724
! 								   ic, FALSE);
634b724
  		    if (type == TYPE_NEQUAL)
634b724
  			n1 = !n1;
634b724
  		}
634b724
***************
634b724
*** 4379,4385 ****
634b724
  		else
634b724
  		{
634b724
  		    /* Compare two Dictionaries for being equal or unequal. */
634b724
! 		    n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
634b724
  		    if (type == TYPE_NEQUAL)
634b724
  			n1 = !n1;
634b724
  		}
634b724
--- 4380,4387 ----
634b724
  		else
634b724
  		{
634b724
  		    /* Compare two Dictionaries for being equal or unequal. */
634b724
! 		    n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
634b724
! 								   ic, FALSE);
634b724
  		    if (type == TYPE_NEQUAL)
634b724
  			n1 = !n1;
634b724
  		}
634b724
***************
634b724
*** 5914,5923 ****
634b724
   * Return TRUE when two lists have exactly the same values.
634b724
   */
634b724
      static int
634b724
! list_equal(l1, l2, ic)
634b724
      list_T	*l1;
634b724
      list_T	*l2;
634b724
      int		ic;	/* ignore case for strings */
634b724
  {
634b724
      listitem_T	*item1, *item2;
634b724
  
634b724
--- 5916,5926 ----
634b724
   * Return TRUE when two lists have exactly the same values.
634b724
   */
634b724
      static int
634b724
! list_equal(l1, l2, ic, recursive)
634b724
      list_T	*l1;
634b724
      list_T	*l2;
634b724
      int		ic;	/* ignore case for strings */
634b724
+     int		recursive;  /* TRUE when used recursively */
634b724
  {
634b724
      listitem_T	*item1, *item2;
634b724
  
634b724
***************
634b724
*** 5931,5937 ****
634b724
      for (item1 = l1->lv_first, item2 = l2->lv_first;
634b724
  	    item1 != NULL && item2 != NULL;
634b724
  			       item1 = item1->li_next, item2 = item2->li_next)
634b724
! 	if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
634b724
  	    return FALSE;
634b724
      return item1 == NULL && item2 == NULL;
634b724
  }
634b724
--- 5934,5940 ----
634b724
      for (item1 = l1->lv_first, item2 = l2->lv_first;
634b724
  	    item1 != NULL && item2 != NULL;
634b724
  			       item1 = item1->li_next, item2 = item2->li_next)
634b724
! 	if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
634b724
  	    return FALSE;
634b724
      return item1 == NULL && item2 == NULL;
634b724
  }
634b724
***************
634b724
*** 5953,5962 ****
634b724
   * Return TRUE when two dictionaries have exactly the same key/values.
634b724
   */
634b724
      static int
634b724
! dict_equal(d1, d2, ic)
634b724
      dict_T	*d1;
634b724
      dict_T	*d2;
634b724
      int		ic;	/* ignore case for strings */
634b724
  {
634b724
      hashitem_T	*hi;
634b724
      dictitem_T	*item2;
634b724
--- 5956,5966 ----
634b724
   * Return TRUE when two dictionaries have exactly the same key/values.
634b724
   */
634b724
      static int
634b724
! dict_equal(d1, d2, ic, recursive)
634b724
      dict_T	*d1;
634b724
      dict_T	*d2;
634b724
      int		ic;	/* ignore case for strings */
634b724
+     int		recursive; /* TRUE when used recursively */
634b724
  {
634b724
      hashitem_T	*hi;
634b724
      dictitem_T	*item2;
634b724
***************
634b724
*** 5977,5983 ****
634b724
  	    item2 = dict_find(d2, hi->hi_key, -1);
634b724
  	    if (item2 == NULL)
634b724
  		return FALSE;
634b724
! 	    if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
634b724
  		return FALSE;
634b724
  	    --todo;
634b724
  	}
634b724
--- 5981,5987 ----
634b724
  	    item2 = dict_find(d2, hi->hi_key, -1);
634b724
  	    if (item2 == NULL)
634b724
  		return FALSE;
634b724
! 	    if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
634b724
  		return FALSE;
634b724
  	    --todo;
634b724
  	}
634b724
***************
634b724
*** 5985,6025 ****
634b724
      return TRUE;
634b724
  }
634b724
  
634b724
  /*
634b724
   * Return TRUE if "tv1" and "tv2" have the same value.
634b724
   * Compares the items just like "==" would compare them, but strings and
634b724
   * numbers are different.  Floats and numbers are also different.
634b724
   */
634b724
      static int
634b724
! tv_equal(tv1, tv2, ic)
634b724
      typval_T *tv1;
634b724
      typval_T *tv2;
634b724
!     int	    ic;	    /* ignore case */
634b724
  {
634b724
      char_u	buf1[NUMBUFLEN], buf2[NUMBUFLEN];
634b724
      char_u	*s1, *s2;
634b724
!     static int  recursive = 0;	    /* cach recursive loops */
634b724
      int		r;
634b724
  
634b724
      if (tv1->v_type != tv2->v_type)
634b724
  	return FALSE;
634b724
      /* Catch lists and dicts that have an endless loop by limiting
634b724
!      * recursiveness to 1000.  We guess they are equal then. */
634b724
!     if (recursive >= 1000)
634b724
  	return TRUE;
634b724
  
634b724
      switch (tv1->v_type)
634b724
      {
634b724
  	case VAR_LIST:
634b724
! 	    ++recursive;
634b724
! 	    r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
634b724
! 	    --recursive;
634b724
  	    return r;
634b724
  
634b724
  	case VAR_DICT:
634b724
! 	    ++recursive;
634b724
! 	    r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
634b724
! 	    --recursive;
634b724
  	    return r;
634b724
  
634b724
  	case VAR_FUNC:
634b724
--- 5989,6042 ----
634b724
      return TRUE;
634b724
  }
634b724
  
634b724
+ static int tv_equal_recurse_limit;
634b724
+ 
634b724
  /*
634b724
   * Return TRUE if "tv1" and "tv2" have the same value.
634b724
   * Compares the items just like "==" would compare them, but strings and
634b724
   * numbers are different.  Floats and numbers are also different.
634b724
   */
634b724
      static int
634b724
! tv_equal(tv1, tv2, ic, recursive)
634b724
      typval_T *tv1;
634b724
      typval_T *tv2;
634b724
!     int	     ic;	    /* ignore case */
634b724
!     int	     recursive;	    /* TRUE when used recursively */
634b724
  {
634b724
      char_u	buf1[NUMBUFLEN], buf2[NUMBUFLEN];
634b724
      char_u	*s1, *s2;
634b724
!     static int  recursive_cnt = 0;	    /* catch recursive loops */
634b724
      int		r;
634b724
  
634b724
      if (tv1->v_type != tv2->v_type)
634b724
  	return FALSE;
634b724
+ 
634b724
      /* Catch lists and dicts that have an endless loop by limiting
634b724
!      * recursiveness to a limit.  We guess they are equal then.
634b724
!      * A fixed limit has the problem of still taking an awful long time.
634b724
!      * Reduce the limit every time running into it. That should work fine for
634b724
!      * deeply linked structures that are not recursively linked and catch
634b724
!      * recursiveness quickly. */
634b724
!     if (!recursive)
634b724
! 	tv_equal_recurse_limit = 1000;
634b724
!     if (recursive_cnt >= tv_equal_recurse_limit)
634b724
!     {
634b724
! 	--tv_equal_recurse_limit;
634b724
  	return TRUE;
634b724
+     }
634b724
  
634b724
      switch (tv1->v_type)
634b724
      {
634b724
  	case VAR_LIST:
634b724
! 	    ++recursive_cnt;
634b724
! 	    r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
634b724
! 	    --recursive_cnt;
634b724
  	    return r;
634b724
  
634b724
  	case VAR_DICT:
634b724
! 	    ++recursive_cnt;
634b724
! 	    r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
634b724
! 	    --recursive_cnt;
634b724
  	    return r;
634b724
  
634b724
  	case VAR_FUNC:
634b724
***************
634b724
*** 9391,9397 ****
634b724
  	    }
634b724
  
634b724
  	    for ( ; li != NULL; li = li->li_next)
634b724
! 		if (tv_equal(&li->li_tv, &argvars[1], ic))
634b724
  		    ++n;
634b724
  	}
634b724
      }
634b724
--- 9408,9414 ----
634b724
  	    }
634b724
  
634b724
  	    for ( ; li != NULL; li = li->li_next)
634b724
! 		if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
634b724
  		    ++n;
634b724
  	}
634b724
      }
634b724
***************
634b724
*** 9418,9424 ****
634b724
  		if (!HASHITEM_EMPTY(hi))
634b724
  		{
634b724
  		    --todo;
634b724
! 		    if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
634b724
  			++n;
634b724
  		}
634b724
  	    }
634b724
--- 9435,9441 ----
634b724
  		if (!HASHITEM_EMPTY(hi))
634b724
  		{
634b724
  		    --todo;
634b724
! 		    if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
634b724
  			++n;
634b724
  		}
634b724
  	    }
634b724
***************
634b724
*** 12574,12580 ****
634b724
  	}
634b724
  
634b724
  	for ( ; item != NULL; item = item->li_next, ++idx)
634b724
! 	    if (tv_equal(&item->li_tv, &argvars[1], ic))
634b724
  	    {
634b724
  		rettv->vval.v_number = idx;
634b724
  		break;
634b724
--- 12591,12597 ----
634b724
  	}
634b724
  
634b724
  	for ( ; item != NULL; item = item->li_next, ++idx)
634b724
! 	    if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
634b724
  	    {
634b724
  		rettv->vval.v_number = idx;
634b724
  		break;
634b724
*** ../vim-7.3.054/src/testdir/test55.in	2010-08-15 21:57:29.000000000 +0200
634b724
--- src/testdir/test55.in	2010-11-10 20:15:27.000000000 +0100
634b724
***************
634b724
*** 342,348 ****
634b724
--- 342,359 ----
634b724
  :$put =(d == d)
634b724
  :$put =(l != deepcopy(l))
634b724
  :$put =(d != deepcopy(d))
634b724
+ :"
634b724
+ :" compare complex recursively linked list and dict
634b724
+ :let l = []
634b724
+ :call add(l, l)
634b724
+ :let dict4 = {"l": l}
634b724
+ :call add(dict4.l, dict4)
634b724
+ :let lcopy = deepcopy(l)
634b724
+ :let dict4copy = deepcopy(dict4)
634b724
+ :$put =(l == lcopy)
634b724
+ :$put =(dict4 == dict4copy)
634b724
  :endfun
634b724
+ :"
634b724
  :call Test(1, 2, [3, 4], {5: 6})  " This may take a while
634b724
  :"
634b724
  :delfunc Test
634b724
*** ../vim-7.3.054/src/testdir/test55.ok	2010-08-15 21:57:29.000000000 +0200
634b724
--- src/testdir/test55.ok	2010-11-10 20:16:37.000000000 +0100
634b724
***************
634b724
*** 109,111 ****
634b724
--- 109,113 ----
634b724
  1
634b724
  0
634b724
  0
634b724
+ 1
634b724
+ 1
634b724
*** ../vim-7.3.054/src/version.c	2010-11-10 18:59:50.000000000 +0100
634b724
--- src/version.c	2010-11-10 20:10:51.000000000 +0100
634b724
***************
634b724
*** 716,717 ****
634b724
--- 716,719 ----
634b724
  {   /* Add new patch number below this line */
634b724
+ /**/
634b724
+     55,
634b724
  /**/
634b724
634b724
-- 
634b724
A special law prohibits unmarried women from parachuting on Sunday or she
634b724
shall risk arrest, fine, and/or jailing.
634b724
		[real standing law in Florida, United States of America]
634b724
634b724
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
634b724
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
634b724
\\\        download, build and distribute -- http://www.A-A-P.org        ///
634b724
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///