Skip to content

Commit 1690032

Browse files
committed
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.
1 parent 45bbaef commit 1690032

File tree

6 files changed

+226
-95
lines changed

6 files changed

+226
-95
lines changed

src/proto/vim9instr.pro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,14 @@ int generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where);
4646
int generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off);
4747
int generate_FOR(cctx_T *cctx, int loop_idx);
4848
int generate_TRYCONT(cctx_T *cctx, int levels, int where);
49+
int check_internal_func_args(cctx_T *cctx, int func_idx, int argcount, int method_call, type2_T **argtypes, type2_T *shuffled_argtypes);
4950
int generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call);
5051
int generate_LISTAPPEND(cctx_T *cctx);
5152
int generate_BLOBAPPEND(cctx_T *cctx);
53+
int check_args_on_stack(cctx_T *cctx, ufunc_T *ufunc, int argcount);
5254
int generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount);
5355
int generate_UCALL(cctx_T *cctx, char_u *name, int argcount);
56+
int check_func_args_from_type(cctx_T *cctx, type_T *type, int argcount, int at_top, char_u *name);
5457
int generate_PCALL(cctx_T *cctx, int argcount, char_u *name, type_T *type, int at_top);
5558
int generate_DEFER(cctx_T *cctx, int var_idx, int argcount);
5659
int generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len);

src/testdir/test_user_func.vim

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
source check.vim
77
source shared.vim
8+
import './vim9.vim' as v9
89

910
func Table(title, ...)
1011
let ret = a:title
@@ -619,7 +620,7 @@ func Test_defer_quitall()
619620
DeferLevelOne()
620621
END
621622
call writefile(lines, 'XdeferQuitall', 'D')
622-
let res = system(GetVimCommandClean() .. ' -X -S XdeferQuitall')
623+
let res = system(GetVimCommand() .. ' -X -S XdeferQuitall')
623624
call assert_equal(0, v:shell_error)
624625
call assert_false(filereadable('XQuitallOne'))
625626
call assert_false(filereadable('XQuitallTwo'))
@@ -641,7 +642,7 @@ func Test_defer_quitall_in_expr_func()
641642
call Test_defer_in_funcref()
642643
END
643644
call writefile(lines, 'XdeferQuitallExpr', 'D')
644-
let res = system(GetVimCommandClean() .. ' -X -S XdeferQuitallExpr')
645+
let res = system(GetVimCommand() .. ' -X -S XdeferQuitallExpr')
645646
call assert_equal(0, v:shell_error)
646647
call assert_false(filereadable('Xentry0'))
647648
call assert_false(filereadable('Xentry1'))
@@ -695,5 +696,60 @@ def Test_defer_in_funcref()
695696
assert_false(filereadable('Xentry2'))
696697
enddef
697698

699+
func Test_defer_wrong_arguments()
700+
call assert_fails('defer delete()', 'E119:')
701+
call assert_fails('defer FuncIndex(1)', 'E119:')
702+
call assert_fails('defer delete(1, 2, 3)', 'E118:')
703+
call assert_fails('defer FuncIndex(1, 2, 3)', 'E118:')
704+
705+
let lines =<< trim END
706+
def DeferFunc0()
707+
defer delete()
708+
enddef
709+
defcompile
710+
END
711+
call v9.CheckScriptFailure(lines, 'E119:')
712+
let lines =<< trim END
713+
def DeferFunc3()
714+
defer delete(1, 2, 3)
715+
enddef
716+
defcompile
717+
END
718+
call v9.CheckScriptFailure(lines, 'E118:')
719+
let lines =<< trim END
720+
def DeferFunc2()
721+
defer delete(1, 2)
722+
enddef
723+
defcompile
724+
END
725+
call v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
726+
727+
def g:FuncOneArg(arg: string)
728+
echo arg
729+
enddef
730+
731+
let lines =<< trim END
732+
def DeferUserFunc0()
733+
defer g:FuncOneArg()
734+
enddef
735+
defcompile
736+
END
737+
call v9.CheckScriptFailure(lines, 'E119:')
738+
let lines =<< trim END
739+
def DeferUserFunc2()
740+
defer g:FuncOneArg(1, 2)
741+
enddef
742+
defcompile
743+
END
744+
call v9.CheckScriptFailure(lines, 'E118:')
745+
let lines =<< trim END
746+
def DeferUserFunc1()
747+
defer g:FuncOneArg(1)
748+
enddef
749+
defcompile
750+
END
751+
call v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
752+
endfunc
753+
698754

699755
" vim: shiftwidth=2 sts=2 expandtab

src/userfunc.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5608,6 +5608,7 @@ ex_call_inner(
56085608
ex_defer_inner(
56095609
char_u *name,
56105610
char_u **arg,
5611+
type_T *type,
56115612
partial_T *partial,
56125613
evalarg_T *evalarg)
56135614
{
@@ -5640,6 +5641,44 @@ ex_defer_inner(
56405641
r = get_func_arguments(arg, evalarg, FALSE,
56415642
argvars + partial_argc, &argcount);
56425643
argcount += partial_argc;
5644+
5645+
if (r == OK)
5646+
{
5647+
if (type != NULL)
5648+
{
5649+
// Check that the arguments are OK for the types of the funcref.
5650+
r = check_argument_types(type, argvars, argcount, NULL, name);
5651+
}
5652+
else if (builtin_function(name, -1))
5653+
{
5654+
int idx = find_internal_func(name);
5655+
5656+
if (idx < 0)
5657+
{
5658+
emsg_funcname(e_unknown_function_str, name);
5659+
r = FAIL;
5660+
}
5661+
else if (check_internal_func(idx, argcount) == -1)
5662+
r = FAIL;
5663+
}
5664+
else
5665+
{
5666+
ufunc_T *ufunc = find_func(name, FALSE);
5667+
5668+
// we tolerate an unknown function here, it might be defined later
5669+
if (ufunc != NULL)
5670+
{
5671+
int error = check_user_func_argcount(ufunc, argcount);
5672+
5673+
if (error != FCERR_UNKNOWN)
5674+
{
5675+
user_func_error(error, name, NULL);
5676+
r = FAIL;
5677+
}
5678+
}
5679+
}
5680+
}
5681+
56435682
if (r == FAIL)
56445683
{
56455684
while (--argcount >= 0)
@@ -5839,7 +5878,7 @@ ex_call(exarg_T *eap)
58395878
if (eap->cmdidx == CMD_defer)
58405879
{
58415880
arg = startarg;
5842-
failed = ex_defer_inner(name, &arg, partial, &evalarg) == FAIL;
5881+
failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
58435882
}
58445883
else
58455884
{

src/version.c

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

704704
static int included_patches[] =
705705
{ /* Add new patch number below this line */
706+
/**/
707+
419,
706708
/**/
707709
418,
708710
/**/

src/vim9cmds.c

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,34 +1684,13 @@ compile_eval(char_u *arg, cctx_T *cctx)
16841684
return skipwhite(p);
16851685
}
16861686

1687-
/*
1688-
* Get the local variable index for deferred function calls.
1689-
* Reserve it when not done already.
1690-
* Returns zero for failure.
1691-
*/
1692-
int
1693-
get_defer_var_idx(cctx_T *cctx)
1694-
{
1695-
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1696-
+ cctx->ctx_ufunc->uf_dfunc_idx;
1697-
if (dfunc->df_defer_var_idx == 0)
1698-
{
1699-
lvar_T *lvar = reserve_local(cctx, (char_u *)"@defer@", 7,
1700-
TRUE, &t_list_any);
1701-
if (lvar == NULL)
1702-
return 0;
1703-
dfunc->df_defer_var_idx = lvar->lv_idx + 1;
1704-
}
1705-
return dfunc->df_defer_var_idx;
1706-
}
1707-
17081687
/*
17091688
* Compile "defer func(arg)".
17101689
*/
17111690
char_u *
17121691
compile_defer(char_u *arg_start, cctx_T *cctx)
17131692
{
1714-
char_u *p;
1693+
char_u *paren;
17151694
char_u *arg = arg_start;
17161695
int argcount = 0;
17171696
int defer_var_idx;
@@ -1720,21 +1699,21 @@ compile_defer(char_u *arg_start, cctx_T *cctx)
17201699

17211700
// Get a funcref for the function name.
17221701
// TODO: better way to find the "(".
1723-
p = vim_strchr(arg, '(');
1724-
if (p == NULL)
1702+
paren = vim_strchr(arg, '(');
1703+
if (paren == NULL)
17251704
{
17261705
semsg(_(e_missing_parenthesis_str), arg);
17271706
return NULL;
17281707
}
1729-
*p = NUL;
1708+
*paren = NUL;
17301709
func_idx = find_internal_func(arg);
17311710
if (func_idx >= 0)
17321711
// TODO: better type
17331712
generate_PUSHFUNC(cctx, (char_u *)internal_func_name(func_idx),
17341713
&t_func_any, FALSE);
17351714
else if (compile_expr0(&arg, cctx) == FAIL)
17361715
return NULL;
1737-
*p = '(';
1716+
*paren = '(';
17381717

17391718
// check for function type
17401719
type = get_type_on_stack(cctx, 0);
@@ -1745,11 +1724,22 @@ compile_defer(char_u *arg_start, cctx_T *cctx)
17451724
}
17461725

17471726
// compile the arguments
1748-
arg = skipwhite(p + 1);
1727+
arg = skipwhite(paren + 1);
17491728
if (compile_arguments(&arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
17501729
return NULL;
17511730

1752-
// TODO: check argument count with "type"
1731+
if (func_idx >= 0)
1732+
{
1733+
type2_T *argtypes = NULL;
1734+
type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1735+
1736+
if (check_internal_func_args(cctx, func_idx, argcount, FALSE,
1737+
&argtypes, shuffled_argtypes) == FAIL)
1738+
return NULL;
1739+
}
1740+
else if (check_func_args_from_type(cctx, type, argcount, TRUE,
1741+
arg_start) == FAIL)
1742+
return NULL;
17531743

17541744
defer_var_idx = get_defer_var_idx(cctx);
17551745
if (defer_var_idx == 0)

0 commit comments

Comments
 (0)