Skip to content

Commit ca81f0e

Browse files
committed
patch 8.2.3023: Vim9: arguments for execute() not checked at compile time
Problem: Vim9: arguments for execute() not checked at compile time. Solution: Add a function to check the argument types.
1 parent f573c6e commit ca81f0e

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/evalfunc.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,27 @@ arg_list_or_blob(type_T *type, argcontext_T *context)
301301
return FAIL;
302302
}
303303

304+
/*
305+
* Check "type" is a string or a list of strings.
306+
*/
307+
static int
308+
arg_string_or_list(type_T *type, argcontext_T *context)
309+
{
310+
if (type->tt_type == VAR_ANY || type->tt_type == VAR_STRING)
311+
return OK;
312+
if (type->tt_type != VAR_LIST)
313+
{
314+
arg_type_mismatch(&t_string, type, context->arg_idx + 1);
315+
return FAIL;
316+
}
317+
if (type->tt_member->tt_type == VAR_ANY
318+
|| type->tt_member->tt_type == VAR_STRING)
319+
return OK;
320+
321+
arg_type_mismatch(&t_list_string, type, context->arg_idx + 1);
322+
return FAIL;
323+
}
324+
304325
/*
305326
* Check "type" is a list or a dict.
306327
*/
@@ -385,6 +406,7 @@ argcheck_T arg1_string[] = {arg_string};
385406
argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
386407
argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
387408
argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
409+
argcheck_T arg2_execute[] = {arg_string_or_list, arg_string};
388410
argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
389411
argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
390412
argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
@@ -870,7 +892,7 @@ static funcentry_T global_functions[] =
870892
ret_number_bool, f_eventhandler},
871893
{"executable", 1, 1, FEARG_1, NULL,
872894
ret_number, f_executable},
873-
{"execute", 1, 2, FEARG_1, NULL,
895+
{"execute", 1, 2, FEARG_1, arg2_execute,
874896
ret_string, f_execute},
875897
{"exepath", 1, 1, FEARG_1, NULL,
876898
ret_string, f_exepath},

src/testdir/test_vim9_builtin.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,18 @@ def Test_executable()
324324
CheckDefExecFailure(['echo executable(true)'], 'E1174:')
325325
enddef
326326

327+
def Test_execute()
328+
var res = execute("echo 'hello'")
329+
assert_equal("\nhello", res)
330+
res = execute(["echo 'here'", "echo 'there'"])
331+
assert_equal("\nhere\nthere", res)
332+
333+
CheckDefFailure(['echo execute(123)'], 'E1013: Argument 1: type mismatch, expected string but got number')
334+
CheckDefFailure(['echo execute([123])'], 'E1013: Argument 1: type mismatch, expected list<string> but got list<number>')
335+
CheckDefExecFailure(['echo execute(["xx", 123])'], 'E492')
336+
CheckDefFailure(['echo execute("xx", 123)'], 'E1013: Argument 2: type mismatch, expected string but got number')
337+
enddef
338+
327339
def Test_exepath()
328340
CheckDefExecFailure(['echo exepath(true)'], 'E1174:')
329341
CheckDefExecFailure(['echo exepath(v:null)'], 'E1174:')

src/version.c

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

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3023,
758760
/**/
759761
3022,
760762
/**/

0 commit comments

Comments
 (0)