4 From: Bram Moolenaar <Bram@moolenaar.net>
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
11 Problem: Crash with specific use of function calls. (Meikel Brandmeyer)
12 Solution: Make sure the items referenced by a function call are not freed
13 twice. (based on patch from Nico Weber)
17 *** ../vim-7.2.187/src/eval.c 2009-05-16 17:29:37.000000000 +0200
18 --- src/eval.c 2009-05-22 20:04:22.000000000 +0200
23 * When recursively copying lists and dicts we need to remember which ones we
24 * have done to avoid endless recursiveness. This unique ID is used for that.
25 + * The last bit is used for previous_funccal, ignored when comparing.
27 static int current_copyID = 0;
28 + #define COPYID_INC 2
29 + #define COPYID_MASK (~0x1)
32 * Array to hold the hashtab with variables local to each sourced script.
36 static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
37 static char_u *list2string __ARGS((typval_T *tv, int copyID));
38 static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
39 + static int free_unref_items __ARGS((int copyID));
40 static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
41 static void set_ref_in_list __ARGS((list_T *l, int copyID));
42 static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
50 ! int copyID = ++current_copyID;
54 funccall_T *fc, **pfc;
55 ! int did_free = FALSE;
67 funccall_T *fc, **pfc;
69 ! int did_free_funccal = FALSE;
76 may_garbage_collect = FALSE;
77 garbage_collect_at_exit = FALSE;
79 + /* We advance by two because we add one for items referenced through
80 + * previous_funccal. */
81 + current_copyID += COPYID_INC;
82 + copyID = current_copyID;
85 * 1. Go through all accessible variables and mark all lists and dicts
89 + /* Don't free variables in the previous_funccal list unless they are only
90 + * referenced through previous_funccal. This must be first, because if
91 + * the item is referenced elsewhere it must not be freed. */
92 + for (fc = previous_funccal; fc != NULL; fc = fc->caller)
94 + set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
95 + set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
98 /* script-local variables */
99 for (i = 1; i <= ga_scripts.ga_len; ++i)
100 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
104 set_ref_in_ht(&vimvarht, copyID);
107 ! * 2. Go through the list of dicts and free items without the copyID.
109 for (dd = first_dict; dd != NULL; )
110 ! if (dd->dv_copyID != copyID)
112 /* Free the Dictionary and ordinary items it contains, but don't
113 * recurse into Lists and Dictionaries, they will be in the list
116 set_ref_in_ht(&vimvarht, copyID);
118 + /* Free lists and dictionaries that are not referenced. */
119 + did_free = free_unref_items(copyID);
121 + /* check if any funccal can be freed now */
122 + for (pfc = &previous_funccal; *pfc != NULL; )
124 + if (can_free_funccal(*pfc, copyID))
128 + free_funccal(fc, TRUE);
130 + did_free_funccal = TRUE;
133 + pfc = &(*pfc)->caller;
135 + if (did_free_funccal)
136 + /* When a funccal was freed some more items might be garbage
137 + * collected, so run again. */
138 + (void)garbage_collect();
144 + * Free lists and dictionaries that are no longer referenced.
147 + free_unref_items(copyID)
152 + int did_free = FALSE;
155 ! * Go through the list of dicts and free items without the copyID.
157 for (dd = first_dict; dd != NULL; )
158 ! if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
160 /* Free the Dictionary and ordinary items it contains, but don't
161 * recurse into Lists and Dictionaries, they will be in the list
164 dd = dd->dv_used_next;
167 ! * 3. Go through the list of lists and free items without the copyID.
168 ! * But don't free a list that has a watcher (used in a for loop), these
169 ! * are not referenced anywhere.
171 for (ll = first_list; ll != NULL; )
172 ! if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
174 /* Free the List and ordinary items it contains, but don't recurse
175 * into Lists and Dictionaries, they will be in the list of dicts
177 dd = dd->dv_used_next;
180 ! * Go through the list of lists and free items without the copyID.
181 ! * But don't free a list that has a watcher (used in a for loop), these
182 ! * are not referenced anywhere.
184 for (ll = first_list; ll != NULL; )
185 ! if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
186 ! && ll->lv_watch == NULL)
188 /* Free the List and ordinary items it contains, but don't recurse
189 * into Lists and Dictionaries, they will be in the list of dicts
193 ll = ll->lv_used_next;
195 - /* check if any funccal can be freed now */
196 - for (pfc = &previous_funccal; *pfc != NULL; )
198 - if (can_free_funccal(*pfc, copyID))
202 - free_funccal(fc, TRUE);
206 - pfc = &(*pfc)->caller;
217 hash_init(&dict->dv_hashtab);
218 dict->dv_refcount = DO_NOT_FREE_CNT;
219 + dict->dv_copyID = 0;
220 dict_var->di_tv.vval.v_dict = dict;
221 dict_var->di_tv.v_type = VAR_DICT;
222 dict_var->di_tv.v_lock = VAR_FIXED;
225 current_funccal = fc->caller;
228 ! /* if the a:000 list and the a: dict are not referenced we can free the
229 ! * funccall_T and what's in it. */
230 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
231 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
232 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
234 current_funccal = fc->caller;
237 ! /* If the a:000 list and the l: and a: dicts are not referenced we can
238 ! * free the funccall_T and what's in it. */
239 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
240 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
241 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
246 * Return TRUE if items in "fc" do not have "copyID". That means they are not
247 ! * referenced from anywhere.
250 can_free_funccal(fc, copyID)
254 * Return TRUE if items in "fc" do not have "copyID". That means they are not
255 ! * referenced from anywhere that is in use.
258 can_free_funccal(fc, copyID)
259 *** ../vim-7.2.187/src/version.c 2009-05-23 14:27:43.000000000 +0200
260 --- src/version.c 2009-05-24 13:20:49.000000000 +0200
264 { /* Add new patch number below this line */
270 ARTHUR: ... and I am your king ....
271 OLD WOMAN: Ooooh! I didn't know we had a king. I thought we were an
272 autonomous collective ...
273 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
275 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
276 /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
277 \\\ download, build and distribute -- http://www.A-A-P.org ///
278 \\\ help me help AIDS victims -- http://ICCF-Holland.org ///