Skip to content

Commit 5d905c2

Browse files
committed
patch 8.2.0513: reading past allocate memory when using varargs
Problem: Reading past allocate memory when using varargs. Solution: Fix copying function argument types.
1 parent 5deeb3f commit 5d905c2

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,8 @@ static char *(features[]) =
738738

739739
static int included_patches[] =
740740
{ /* Add new patch number below this line */
741+
/**/
742+
513,
741743
/**/
742744
512,
743745
/**/

src/vim9compile.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
353353
}
354354

355355
/*
356-
* For a function type, reserve space for "argcount" argument types.
356+
* For a function type, reserve space for "argcount" argument types (including
357+
* vararg).
357358
*/
358359
static int
359360
func_type_add_arg_types(
@@ -5823,16 +5824,19 @@ compile_def_function(ufunc_T *ufunc, int set_return_type)
58235824
}
58245825

58255826
{
5826-
int argcount = ufunc->uf_args.ga_len
5827-
+ (ufunc->uf_va_name == NULL ? 0 : 1);
5827+
int varargs = ufunc->uf_va_name != NULL;
5828+
int argcount = ufunc->uf_args.ga_len - (varargs ? 1 : 0);
58285829

58295830
// Create a type for the function, with the return type and any
58305831
// argument types.
5831-
ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, argcount,
5832-
&ufunc->uf_type_list);
5833-
if (argcount > 0)
5832+
// A vararg is included in uf_args.ga_len but not in uf_arg_types.
5833+
// The type is included in "tt_args".
5834+
ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5835+
ufunc->uf_args.ga_len, &ufunc->uf_type_list);
5836+
if (ufunc->uf_args.ga_len > 0)
58345837
{
5835-
if (func_type_add_arg_types(ufunc->uf_func_type, argcount,
5838+
if (func_type_add_arg_types(ufunc->uf_func_type,
5839+
ufunc->uf_args.ga_len,
58365840
argcount - ufunc->uf_def_args.ga_len,
58375841
&ufunc->uf_type_list) == FAIL)
58385842
{
@@ -5850,6 +5854,9 @@ compile_def_function(ufunc_T *ufunc, int set_return_type)
58505854
else
58515855
mch_memmove(ufunc->uf_func_type->tt_args,
58525856
ufunc->uf_arg_types, sizeof(type_T *) * argcount);
5857+
if (varargs)
5858+
ufunc->uf_func_type->tt_args[argcount] =
5859+
ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
58535860
}
58545861
}
58555862

0 commit comments

Comments
 (0)