Skip to content

Commit 32105ae

Browse files
committed
patch 8.2.2664: Vim9: not enough function arguments checked for string
Problem: Vim9: not enough function arguments checked for string. Solution: Check in balloon functions. Refactor function arguments.
1 parent 79efa2e commit 32105ae

File tree

7 files changed

+58
-29
lines changed

7 files changed

+58
-29
lines changed

src/evalfunc.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,8 +2323,12 @@ f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
23232323
}
23242324
else
23252325
{
2326-
char_u *mesg = tv_get_string_chk(&argvars[0]);
2326+
char_u *mesg;
23272327

2328+
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2329+
return;
2330+
2331+
mesg = tv_get_string_chk(&argvars[0]);
23282332
if (mesg != NULL)
23292333
// empty string removes the balloon
23302334
post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
@@ -2338,8 +2342,11 @@ f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
23382342
{
23392343
if (rettv_list_alloc(rettv) == OK)
23402344
{
2341-
char_u *msg = tv_get_string_chk(&argvars[0]);
2345+
char_u *msg;
23422346

2347+
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2348+
return;
2349+
msg = tv_get_string_chk(&argvars[0]);
23432350
if (msg != NULL)
23442351
{
23452352
pumitem_T *array;

src/filepath.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ f_delete(typval_T *argvars, typval_T *rettv)
861861
void
862862
f_executable(typval_T *argvars, typval_T *rettv)
863863
{
864-
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL)
864+
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
865865
return;
866866

867867
// Check in $PATH and also check directly if there is a directory name.
@@ -876,7 +876,7 @@ f_exepath(typval_T *argvars, typval_T *rettv)
876876
{
877877
char_u *p = NULL;
878878

879-
if (in_vim9script() && check_for_nonempty_string(&argvars[0], 1) == FAIL)
879+
if (in_vim9script() && check_for_nonempty_string_arg(argvars, 0) == FAIL)
880880
return;
881881
(void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
882882
rettv->v_type = VAR_STRING;
@@ -893,7 +893,7 @@ f_filereadable(typval_T *argvars, typval_T *rettv)
893893
char_u *p;
894894
int n;
895895

896-
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL)
896+
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
897897
return;
898898
#ifndef O_NONBLOCK
899899
# define O_NONBLOCK 0
@@ -918,7 +918,7 @@ f_filereadable(typval_T *argvars, typval_T *rettv)
918918
void
919919
f_filewritable(typval_T *argvars, typval_T *rettv)
920920
{
921-
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL)
921+
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
922922
return;
923923
rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
924924
}
@@ -942,7 +942,7 @@ findfilendir(
942942

943943
rettv->vval.v_string = NULL;
944944
rettv->v_type = VAR_STRING;
945-
if (in_vim9script() && check_for_nonempty_string(&argvars[0], 1) == FAIL)
945+
if (in_vim9script() && check_for_nonempty_string_arg(argvars, 0) == FAIL)
946946
return;
947947

948948
#ifdef FEAT_SEARCHPATH
@@ -1023,8 +1023,8 @@ f_fnamemodify(typval_T *argvars, typval_T *rettv)
10231023
char_u *fbuf = NULL;
10241024
char_u buf[NUMBUFLEN];
10251025

1026-
if (in_vim9script() && (check_for_string(&argvars[0], 1) == FAIL
1027-
|| check_for_string(&argvars[1], 2) == FAIL))
1026+
if (in_vim9script() && (check_for_string_arg(argvars, 0) == FAIL
1027+
|| check_for_string_arg(argvars, 1) == FAIL))
10281028
return;
10291029
fname = tv_get_string_chk(&argvars[0]);
10301030
mods = tv_get_string_buf_chk(&argvars[1], buf);
@@ -1135,7 +1135,7 @@ f_getfperm(typval_T *argvars, typval_T *rettv)
11351135
char_u *perm = NULL;
11361136
char_u permbuf[] = "---------";
11371137

1138-
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL)
1138+
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
11391139
return;
11401140
fname = tv_get_string(&argvars[0]);
11411141

@@ -1154,7 +1154,7 @@ f_getfsize(typval_T *argvars, typval_T *rettv)
11541154
char_u *fname;
11551155
stat_T st;
11561156

1157-
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL)
1157+
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
11581158
return;
11591159

11601160
fname = tv_get_string(&argvars[0]);
@@ -1184,7 +1184,7 @@ f_getftime(typval_T *argvars, typval_T *rettv)
11841184
char_u *fname;
11851185
stat_T st;
11861186

1187-
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL)
1187+
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
11881188
return;
11891189
fname = tv_get_string(&argvars[0]);
11901190
if (mch_stat((char *)fname, &st) >= 0)
@@ -1230,7 +1230,7 @@ f_getftype(typval_T *argvars, typval_T *rettv)
12301230
stat_T st;
12311231
char_u *type = NULL;
12321232

1233-
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL)
1233+
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
12341234
return;
12351235
fname = tv_get_string(&argvars[0]);
12361236

@@ -2411,9 +2411,9 @@ f_browse(typval_T *argvars UNUSED, typval_T *rettv)
24112411
int error = FALSE;
24122412

24132413
if (in_vim9script()
2414-
&& (check_for_string(&argvars[1], 2) == FAIL
2415-
|| check_for_string(&argvars[2], 3) == FAIL
2416-
|| check_for_string(&argvars[3], 4) == FAIL))
2414+
&& (check_for_string_arg(argvars, 1) == FAIL
2415+
|| check_for_string_arg(argvars, 2) == FAIL
2416+
|| check_for_string_arg(argvars, 3) == FAIL))
24172417
return;
24182418
save = (int)tv_get_number_chk(&argvars[0], &error);
24192419
title = tv_get_string_chk(&argvars[1]);

src/mbyte.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5551,7 +5551,7 @@ f_setcellwidths(typval_T *argvars, typval_T *rettv UNUSED)
55515551
void
55525552
f_charclass(typval_T *argvars, typval_T *rettv UNUSED)
55535553
{
5554-
if (check_for_string(&argvars[0], 1) == FAIL)
5554+
if (check_for_string_arg(argvars, 0) == FAIL)
55555555
return;
55565556
rettv->vval.v_number = mb_get_class(argvars[0].vval.v_string);
55575557
}

src/proto/typval.pro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ varnumber_T tv_get_number_chk(typval_T *varp, int *denote);
99
varnumber_T tv_get_bool(typval_T *varp);
1010
varnumber_T tv_get_bool_chk(typval_T *varp, int *denote);
1111
float_T tv_get_float(typval_T *varp);
12-
int check_for_string(typval_T *tv, int arg);
13-
int check_for_nonempty_string(typval_T *tv, int arg);
12+
int check_for_string_arg(typval_T *args, int idx);
13+
int check_for_nonempty_string_arg(typval_T *args, int idx);
1414
char_u *tv_get_string(typval_T *varp);
1515
char_u *tv_get_string_strict(typval_T *varp);
1616
char_u *tv_get_string_buf(typval_T *varp, char_u *buf);

src/testdir/test_vim9_builtin.vim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ def Test_append()
125125
assert_equal(['0', 'one', '1', 'two', '2'], getline(1, 6))
126126
enddef
127127

128+
def Test_balloon_show()
129+
CheckGui
130+
CheckFeature balloon_eval
131+
132+
assert_fails('balloon_show(true)', 'E1174:')
133+
enddef
134+
135+
def Test_balloon_split()
136+
CheckFeature balloon_eval
137+
138+
assert_fails('balloon_split(true)', 'E1174:')
139+
enddef
140+
128141
def Test_browse()
129142
CheckFeature browse
130143

@@ -142,9 +155,14 @@ def Test_browse()
142155
CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 4')
143156
enddef
144157

158+
def Test_bufexists()
159+
assert_fails('bufexists(true)', 'E1174')
160+
enddef
161+
145162
def Test_buflisted()
146163
var res: bool = buflisted('asdf')
147164
assert_equal(false, res)
165+
assert_fails('buflisted(true)', 'E1174')
148166
enddef
149167

150168
def Test_bufname()
@@ -176,6 +194,8 @@ def Test_bufwinid()
176194
only
177195
bwipe SomeFile
178196
bwipe OtherFile
197+
198+
assert_fails('bufwinid(true)', 'E1138')
179199
enddef
180200

181201
def Test_call_call()

src/typval.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,12 @@ tv_get_float(typval_T *varp)
344344
* Give an error and return FAIL unless "tv" is a string.
345345
*/
346346
int
347-
check_for_string(typval_T *tv, int arg)
347+
check_for_string_arg(typval_T *args, int idx)
348348
{
349-
if (tv->v_type != VAR_STRING)
349+
if (args[idx].v_type != VAR_STRING)
350350
{
351-
if (arg > 0)
352-
semsg(_(e_string_required_for_argument_nr), arg);
351+
if (idx >= 0)
352+
semsg(_(e_string_required_for_argument_nr), idx + 1);
353353
else
354354
emsg(_(e_stringreq));
355355
return FAIL;
@@ -358,17 +358,17 @@ check_for_string(typval_T *tv, int arg)
358358
}
359359

360360
/*
361-
* Give an error and return FAIL unless "tv" is a non-empty string.
361+
* Give an error and return FAIL unless "args[idx]" is a non-empty string.
362362
*/
363363
int
364-
check_for_nonempty_string(typval_T *tv, int arg)
364+
check_for_nonempty_string_arg(typval_T *args, int idx)
365365
{
366-
if (check_for_string(tv, arg) == FAIL)
366+
if (check_for_string_arg(args, idx) == FAIL)
367367
return FAIL;
368-
if (tv->vval.v_string == NULL || *tv->vval.v_string == NUL)
368+
if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
369369
{
370-
if (arg > 0)
371-
semsg(_(e_non_empty_string_required_for_argument_nr), arg);
370+
if (idx >= 0)
371+
semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
372372
else
373373
emsg(_(e_non_empty_string_required));
374374
return FAIL;

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+
2664,
753755
/**/
754756
2663,
755757
/**/

0 commit comments

Comments
 (0)