Skip to content

Commit 04947cc

Browse files
committed
patch 8.2.2574: Vim9: crash when calling partial with wrong function
Problem: Vim9: crash when calling partial with wrong function. Solution: Check argument types of called function. (closes #7912)
1 parent a974953 commit 04947cc

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/testdir/test_vim9_func.vim

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,30 @@ def Test_nested_lambda_in_closure()
23672367
delete('XnestedDone')
23682368
enddef
23692369

2370+
def Test_check_func_arg_types()
2371+
var lines =<< trim END
2372+
vim9script
2373+
def F1(x: string): string
2374+
return x
2375+
enddef
2376+
2377+
def F2(x: number): number
2378+
return x + 1
2379+
enddef
2380+
2381+
def G(g: func): dict<func>
2382+
return {f: g}
2383+
enddef
2384+
2385+
def H(d: dict<func>): string
2386+
return d.f('a')
2387+
enddef
2388+
END
2389+
2390+
CheckScriptSuccess(lines + ['echo H(G(F1))'])
2391+
CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
2392+
enddef
2393+
23702394

23712395

23722396
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

src/version.c

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

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2574,
753755
/**/
754756
2573,
755757
/**/

src/vim9execute.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,27 @@ call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
797797
}
798798

799799
if (ufunc != NULL)
800+
{
801+
if (ufunc->uf_arg_types != NULL)
802+
{
803+
int i;
804+
typval_T *argv = STACK_TV_BOT(0) - argcount;
805+
806+
// The function can change at runtime, check that the argument
807+
// types are correct.
808+
for (i = 0; i < argcount; ++i)
809+
{
810+
type_T *type = i < ufunc->uf_args.ga_len
811+
? ufunc->uf_arg_types[i] : ufunc->uf_va_type;
812+
813+
if (type != NULL && check_typval_arg_type(type,
814+
&argv[i], i + 1) == FAIL)
815+
return FAIL;
816+
}
817+
}
818+
800819
return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
820+
}
801821

802822
return FAIL;
803823
}

0 commit comments

Comments
 (0)