Problem: Memory leak in evalvars.c on alloc failure (Ao Xijie)
Solution: free new_tuple when tuple_append_tv() fails and clear typeval,
add a test.
closes: #20668
Supported by AI.
Signed-off-by: Christian Brabandt <cb@256bit.org>
aid_blob_alloc,
aid_get_func,
aid_defer,
+ aid_tuple_append,
aid_last
} alloc_id_T;
copy_tv(TUPLE_ITEM(tuple, idx), &new_tv);
if (tuple_append_tv(new_tuple, &new_tv) == FAIL)
+ {
+ clear_tv(&new_tv);
+ tuple_unref(new_tuple);
return FAIL;
+ }
idx++;
}
call v9.CheckSourceScriptSuccess(lines)
endfunc
+" Memory allocation failure while collecting the rest of a tuple in an unpack
+func Test_tuple_unpack_memory_fail()
+ let t = ('1', '2', '3')
+ call test_alloc_fail(GetAllocId('tuple_append'), 0, 0)
+ call assert_fails('let [a; rest] = t', 'E342:')
+ unlet t
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab
int
tuple_append_tv(tuple_T *tuple, typval_T *tv)
{
- if (ga_grow(&tuple->tv_items, 1) == FAIL)
+ if (ga_grow_id(&tuple->tv_items, 1, aid_tuple_append) == FAIL)
return FAIL;
tuple_set_item(tuple, TUPLE_LEN(tuple), tv);
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 780,
/**/
779,
/**/