patch 9.0.0419: the :defer command does not check the function arguments

Problem:    The :defer command does not check the function argument count and
            types.
Solution:   Check the function arguments when adding a deferred function.
This commit is contained in:
Bram Moolenaar
2022-09-08 19:51:45 +01:00
parent 45bbaef038
commit 169003289f
6 changed files with 242 additions and 111 deletions

View File

@ -1684,34 +1684,13 @@ compile_eval(char_u *arg, cctx_T *cctx)
return skipwhite(p);
}
/*
* Get the local variable index for deferred function calls.
* Reserve it when not done already.
* Returns zero for failure.
*/
int
get_defer_var_idx(cctx_T *cctx)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ cctx->ctx_ufunc->uf_dfunc_idx;
if (dfunc->df_defer_var_idx == 0)
{
lvar_T *lvar = reserve_local(cctx, (char_u *)"@defer@", 7,
TRUE, &t_list_any);
if (lvar == NULL)
return 0;
dfunc->df_defer_var_idx = lvar->lv_idx + 1;
}
return dfunc->df_defer_var_idx;
}
/*
* Compile "defer func(arg)".
*/
char_u *
compile_defer(char_u *arg_start, cctx_T *cctx)
{
char_u *p;
char_u *paren;
char_u *arg = arg_start;
int argcount = 0;
int defer_var_idx;
@ -1720,13 +1699,13 @@ compile_defer(char_u *arg_start, cctx_T *cctx)
// Get a funcref for the function name.
// TODO: better way to find the "(".
p = vim_strchr(arg, '(');
if (p == NULL)
paren = vim_strchr(arg, '(');
if (paren == NULL)
{
semsg(_(e_missing_parenthesis_str), arg);
return NULL;
}
*p = NUL;
*paren = NUL;
func_idx = find_internal_func(arg);
if (func_idx >= 0)
// TODO: better type
@ -1734,7 +1713,7 @@ compile_defer(char_u *arg_start, cctx_T *cctx)
&t_func_any, FALSE);
else if (compile_expr0(&arg, cctx) == FAIL)
return NULL;
*p = '(';
*paren = '(';
// check for function type
type = get_type_on_stack(cctx, 0);
@ -1745,11 +1724,22 @@ compile_defer(char_u *arg_start, cctx_T *cctx)
}
// compile the arguments
arg = skipwhite(p + 1);
arg = skipwhite(paren + 1);
if (compile_arguments(&arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
return NULL;
// TODO: check argument count with "type"
if (func_idx >= 0)
{
type2_T *argtypes = NULL;
type2_T shuffled_argtypes[MAX_FUNC_ARGS];
if (check_internal_func_args(cctx, func_idx, argcount, FALSE,
&argtypes, shuffled_argtypes) == FAIL)
return NULL;
}
else if (check_func_args_from_type(cctx, type, argcount, TRUE,
arg_start) == FAIL)
return NULL;
defer_var_idx = get_defer_var_idx(cctx);
if (defer_var_idx == 0)