summaryrefslogtreecommitdiffstats
path: root/source/ap/vim/patches/7.2.188
blob: 1aa527dac66b4a7f9942f741d1514009ff22aad0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
To: vim-dev@vim.org
Subject: Patch 7.2.188
Fcc: outbox
From: Bram Moolenaar <Bram@moolenaar.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
------------

Patch 7.2.188
Problem:    Crash with specific use of function calls. (Meikel Brandmeyer)
Solution:   Make sure the items referenced by a function call are not freed
	    twice.  (based on patch from Nico Weber)
Files:	    src/eval.c


*** ../vim-7.2.187/src/eval.c	2009-05-16 17:29:37.000000000 +0200
--- src/eval.c	2009-05-22 20:04:22.000000000 +0200
***************
*** 129,136 ****
--- 129,139 ----
  /*
   * When recursively copying lists and dicts we need to remember which ones we
   * have done to avoid endless recursiveness.  This unique ID is used for that.
+  * The last bit is used for previous_funccal, ignored when comparing.
   */
  static int current_copyID = 0;
+ #define COPYID_INC 2
+ #define COPYID_MASK (~0x1)
  
  /*
   * Array to hold the hashtab with variables local to each sourced script.
***************
*** 439,444 ****
--- 442,448 ----
  static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
  static char_u *list2string __ARGS((typval_T *tv, int copyID));
  static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
+ static int free_unref_items __ARGS((int copyID));
  static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
  static void set_ref_in_list __ARGS((list_T *l, int copyID));
  static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
***************
*** 6494,6507 ****
      int
  garbage_collect()
  {
!     dict_T	*dd;
!     list_T	*ll;
!     int		copyID = ++current_copyID;
      buf_T	*buf;
      win_T	*wp;
      int		i;
      funccall_T	*fc, **pfc;
!     int		did_free = FALSE;
  #ifdef FEAT_WINDOWS
      tabpage_T	*tp;
  #endif
--- 6498,6510 ----
      int
  garbage_collect()
  {
!     int		copyID;
      buf_T	*buf;
      win_T	*wp;
      int		i;
      funccall_T	*fc, **pfc;
!     int		did_free;
!     int		did_free_funccal = FALSE;
  #ifdef FEAT_WINDOWS
      tabpage_T	*tp;
  #endif
***************
*** 6511,6520 ****
--- 6514,6538 ----
      may_garbage_collect = FALSE;
      garbage_collect_at_exit = FALSE;
  
+     /* We advance by two because we add one for items referenced through
+      * previous_funccal. */
+     current_copyID += COPYID_INC;
+     copyID = current_copyID;
+ 
      /*
       * 1. Go through all accessible variables and mark all lists and dicts
       *    with copyID.
       */
+ 
+     /* Don't free variables in the previous_funccal list unless they are only
+      * referenced through previous_funccal.  This must be first, because if
+      * the item is referenced elsewhere it must not be freed. */
+     for (fc = previous_funccal; fc != NULL; fc = fc->caller)
+     {
+ 	set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
+ 	set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
+     }
+ 
      /* script-local variables */
      for (i = 1; i <= ga_scripts.ga_len; ++i)
  	set_ref_in_ht(&SCRIPT_VARS(i), copyID);
***************
*** 6546,6556 ****
      /* v: vars */
      set_ref_in_ht(&vimvarht, copyID);
  
      /*
!      * 2. Go through the list of dicts and free items without the copyID.
       */
      for (dd = first_dict; dd != NULL; )
! 	if (dd->dv_copyID != copyID)
  	{
  	    /* Free the Dictionary and ordinary items it contains, but don't
  	     * recurse into Lists and Dictionaries, they will be in the list
--- 6564,6610 ----
      /* v: vars */
      set_ref_in_ht(&vimvarht, copyID);
  
+     /* Free lists and dictionaries that are not referenced. */
+     did_free = free_unref_items(copyID);
+ 
+     /* check if any funccal can be freed now */
+     for (pfc = &previous_funccal; *pfc != NULL; )
+     {
+ 	if (can_free_funccal(*pfc, copyID))
+ 	{
+ 	    fc = *pfc;
+ 	    *pfc = fc->caller;
+ 	    free_funccal(fc, TRUE);
+ 	    did_free = TRUE;
+ 	    did_free_funccal = TRUE;
+ 	}
+ 	else
+ 	    pfc = &(*pfc)->caller;
+     }
+     if (did_free_funccal)
+ 	/* When a funccal was freed some more items might be garbage
+ 	 * collected, so run again. */
+ 	(void)garbage_collect();
+ 
+     return did_free;
+ }
+ 
+ /*
+  * Free lists and dictionaries that are no longer referenced.
+  */
+     static int
+ free_unref_items(copyID)
+     int copyID;
+ {
+     dict_T	*dd;
+     list_T	*ll;
+     int		did_free = FALSE;
+ 
      /*
!      * Go through the list of dicts and free items without the copyID.
       */
      for (dd = first_dict; dd != NULL; )
! 	if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
  	{
  	    /* Free the Dictionary and ordinary items it contains, but don't
  	     * recurse into Lists and Dictionaries, they will be in the list
***************
*** 6565,6576 ****
  	    dd = dd->dv_used_next;
  
      /*
!      * 3. Go through the list of lists and free items without the copyID.
!      *    But don't free a list that has a watcher (used in a for loop), these
!      *    are not referenced anywhere.
       */
      for (ll = first_list; ll != NULL; )
! 	if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
  	{
  	    /* Free the List and ordinary items it contains, but don't recurse
  	     * into Lists and Dictionaries, they will be in the list of dicts
--- 6619,6631 ----
  	    dd = dd->dv_used_next;
  
      /*
!      * Go through the list of lists and free items without the copyID.
!      * But don't free a list that has a watcher (used in a for loop), these
!      * are not referenced anywhere.
       */
      for (ll = first_list; ll != NULL; )
! 	if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
! 						      && ll->lv_watch == NULL)
  	{
  	    /* Free the List and ordinary items it contains, but don't recurse
  	     * into Lists and Dictionaries, they will be in the list of dicts
***************
*** 6584,6603 ****
  	else
  	    ll = ll->lv_used_next;
  
-     /* check if any funccal can be freed now */
-     for (pfc = &previous_funccal; *pfc != NULL; )
-     {
- 	if (can_free_funccal(*pfc, copyID))
- 	{
- 	    fc = *pfc;
- 	    *pfc = fc->caller;
- 	    free_funccal(fc, TRUE);
- 	    did_free = TRUE;
- 	}
- 	else
- 	    pfc = &(*pfc)->caller;
-     }
- 
      return did_free;
  }
  
--- 6639,6644 ----
***************
*** 18842,18847 ****
--- 18883,18889 ----
  {
      hash_init(&dict->dv_hashtab);
      dict->dv_refcount = DO_NOT_FREE_CNT;
+     dict->dv_copyID = 0;
      dict_var->di_tv.vval.v_dict = dict;
      dict_var->di_tv.v_type = VAR_DICT;
      dict_var->di_tv.v_lock = VAR_FIXED;
***************
*** 21294,21301 ****
      current_funccal = fc->caller;
      --depth;
  
!     /* if the a:000 list and the a: dict are not referenced we can free the
!      * funccall_T and what's in it. */
      if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
  	    && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
  	    && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
--- 21336,21343 ----
      current_funccal = fc->caller;
      --depth;
  
!     /* If the a:000 list and the l: and a: dicts are not referenced we can
!      * free the funccall_T and what's in it. */
      if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
  	    && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
  	    && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
***************
*** 21334,21340 ****
  
  /*
   * Return TRUE if items in "fc" do not have "copyID".  That means they are not
!  * referenced from anywhere.
   */
      static int
  can_free_funccal(fc, copyID)
--- 21376,21382 ----
  
  /*
   * Return TRUE if items in "fc" do not have "copyID".  That means they are not
!  * referenced from anywhere that is in use.
   */
      static int
  can_free_funccal(fc, copyID)
*** ../vim-7.2.187/src/version.c	2009-05-23 14:27:43.000000000 +0200
--- src/version.c	2009-05-24 13:20:49.000000000 +0200
***************
*** 678,679 ****
--- 678,681 ----
  {   /* Add new patch number below this line */
+ /**/
+     188,
  /**/

-- 
ARTHUR:    ... and I am your king ....
OLD WOMAN: Ooooh!  I didn't know we had a king.  I thought we were an
           autonomous collective ...
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\        download, build and distribute -- http://www.A-A-P.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///