0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

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.
This commit is contained in:
Bram Moolenaar 2021-03-27 18:59:25 +01:00
parent 79efa2e3b3
commit 32105ae88f
7 changed files with 58 additions and 29 deletions

View File

@ -2323,8 +2323,12 @@ f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
} }
else else
{ {
char_u *mesg = tv_get_string_chk(&argvars[0]); char_u *mesg;
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
return;
mesg = tv_get_string_chk(&argvars[0]);
if (mesg != NULL) if (mesg != NULL)
// empty string removes the balloon // empty string removes the balloon
post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL); post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
@ -2338,8 +2342,11 @@ f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
{ {
if (rettv_list_alloc(rettv) == OK) if (rettv_list_alloc(rettv) == OK)
{ {
char_u *msg = tv_get_string_chk(&argvars[0]); char_u *msg;
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
return;
msg = tv_get_string_chk(&argvars[0]);
if (msg != NULL) if (msg != NULL)
{ {
pumitem_T *array; pumitem_T *array;

View File

@ -861,7 +861,7 @@ f_delete(typval_T *argvars, typval_T *rettv)
void void
f_executable(typval_T *argvars, typval_T *rettv) f_executable(typval_T *argvars, typval_T *rettv)
{ {
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL) if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
return; return;
// Check in $PATH and also check directly if there is a directory name. // 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)
{ {
char_u *p = NULL; char_u *p = NULL;
if (in_vim9script() && check_for_nonempty_string(&argvars[0], 1) == FAIL) if (in_vim9script() && check_for_nonempty_string_arg(argvars, 0) == FAIL)
return; return;
(void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE); (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
rettv->v_type = VAR_STRING; rettv->v_type = VAR_STRING;
@ -893,7 +893,7 @@ f_filereadable(typval_T *argvars, typval_T *rettv)
char_u *p; char_u *p;
int n; int n;
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL) if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
return; return;
#ifndef O_NONBLOCK #ifndef O_NONBLOCK
# define O_NONBLOCK 0 # define O_NONBLOCK 0
@ -918,7 +918,7 @@ f_filereadable(typval_T *argvars, typval_T *rettv)
void void
f_filewritable(typval_T *argvars, typval_T *rettv) f_filewritable(typval_T *argvars, typval_T *rettv)
{ {
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL) if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
return; return;
rettv->vval.v_number = filewritable(tv_get_string(&argvars[0])); rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
} }
@ -942,7 +942,7 @@ findfilendir(
rettv->vval.v_string = NULL; rettv->vval.v_string = NULL;
rettv->v_type = VAR_STRING; rettv->v_type = VAR_STRING;
if (in_vim9script() && check_for_nonempty_string(&argvars[0], 1) == FAIL) if (in_vim9script() && check_for_nonempty_string_arg(argvars, 0) == FAIL)
return; return;
#ifdef FEAT_SEARCHPATH #ifdef FEAT_SEARCHPATH
@ -1023,8 +1023,8 @@ f_fnamemodify(typval_T *argvars, typval_T *rettv)
char_u *fbuf = NULL; char_u *fbuf = NULL;
char_u buf[NUMBUFLEN]; char_u buf[NUMBUFLEN];
if (in_vim9script() && (check_for_string(&argvars[0], 1) == FAIL if (in_vim9script() && (check_for_string_arg(argvars, 0) == FAIL
|| check_for_string(&argvars[1], 2) == FAIL)) || check_for_string_arg(argvars, 1) == FAIL))
return; return;
fname = tv_get_string_chk(&argvars[0]); fname = tv_get_string_chk(&argvars[0]);
mods = tv_get_string_buf_chk(&argvars[1], buf); mods = tv_get_string_buf_chk(&argvars[1], buf);
@ -1135,7 +1135,7 @@ f_getfperm(typval_T *argvars, typval_T *rettv)
char_u *perm = NULL; char_u *perm = NULL;
char_u permbuf[] = "---------"; char_u permbuf[] = "---------";
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL) if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
return; return;
fname = tv_get_string(&argvars[0]); fname = tv_get_string(&argvars[0]);
@ -1154,7 +1154,7 @@ f_getfsize(typval_T *argvars, typval_T *rettv)
char_u *fname; char_u *fname;
stat_T st; stat_T st;
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL) if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
return; return;
fname = tv_get_string(&argvars[0]); fname = tv_get_string(&argvars[0]);
@ -1184,7 +1184,7 @@ f_getftime(typval_T *argvars, typval_T *rettv)
char_u *fname; char_u *fname;
stat_T st; stat_T st;
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL) if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
return; return;
fname = tv_get_string(&argvars[0]); fname = tv_get_string(&argvars[0]);
if (mch_stat((char *)fname, &st) >= 0) if (mch_stat((char *)fname, &st) >= 0)
@ -1230,7 +1230,7 @@ f_getftype(typval_T *argvars, typval_T *rettv)
stat_T st; stat_T st;
char_u *type = NULL; char_u *type = NULL;
if (in_vim9script() && check_for_string(&argvars[0], 1) == FAIL) if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
return; return;
fname = tv_get_string(&argvars[0]); fname = tv_get_string(&argvars[0]);
@ -2411,9 +2411,9 @@ f_browse(typval_T *argvars UNUSED, typval_T *rettv)
int error = FALSE; int error = FALSE;
if (in_vim9script() if (in_vim9script()
&& (check_for_string(&argvars[1], 2) == FAIL && (check_for_string_arg(argvars, 1) == FAIL
|| check_for_string(&argvars[2], 3) == FAIL || check_for_string_arg(argvars, 2) == FAIL
|| check_for_string(&argvars[3], 4) == FAIL)) || check_for_string_arg(argvars, 3) == FAIL))
return; return;
save = (int)tv_get_number_chk(&argvars[0], &error); save = (int)tv_get_number_chk(&argvars[0], &error);
title = tv_get_string_chk(&argvars[1]); title = tv_get_string_chk(&argvars[1]);

View File

@ -5551,7 +5551,7 @@ f_setcellwidths(typval_T *argvars, typval_T *rettv UNUSED)
void void
f_charclass(typval_T *argvars, typval_T *rettv UNUSED) f_charclass(typval_T *argvars, typval_T *rettv UNUSED)
{ {
if (check_for_string(&argvars[0], 1) == FAIL) if (check_for_string_arg(argvars, 0) == FAIL)
return; return;
rettv->vval.v_number = mb_get_class(argvars[0].vval.v_string); rettv->vval.v_number = mb_get_class(argvars[0].vval.v_string);
} }

View File

@ -9,8 +9,8 @@ varnumber_T tv_get_number_chk(typval_T *varp, int *denote);
varnumber_T tv_get_bool(typval_T *varp); varnumber_T tv_get_bool(typval_T *varp);
varnumber_T tv_get_bool_chk(typval_T *varp, int *denote); varnumber_T tv_get_bool_chk(typval_T *varp, int *denote);
float_T tv_get_float(typval_T *varp); float_T tv_get_float(typval_T *varp);
int check_for_string(typval_T *tv, int arg); int check_for_string_arg(typval_T *args, int idx);
int check_for_nonempty_string(typval_T *tv, int arg); int check_for_nonempty_string_arg(typval_T *args, int idx);
char_u *tv_get_string(typval_T *varp); char_u *tv_get_string(typval_T *varp);
char_u *tv_get_string_strict(typval_T *varp); char_u *tv_get_string_strict(typval_T *varp);
char_u *tv_get_string_buf(typval_T *varp, char_u *buf); char_u *tv_get_string_buf(typval_T *varp, char_u *buf);

View File

@ -125,6 +125,19 @@ def Test_append()
assert_equal(['0', 'one', '1', 'two', '2'], getline(1, 6)) assert_equal(['0', 'one', '1', 'two', '2'], getline(1, 6))
enddef enddef
def Test_balloon_show()
CheckGui
CheckFeature balloon_eval
assert_fails('balloon_show(true)', 'E1174:')
enddef
def Test_balloon_split()
CheckFeature balloon_eval
assert_fails('balloon_split(true)', 'E1174:')
enddef
def Test_browse() def Test_browse()
CheckFeature browse CheckFeature browse
@ -142,9 +155,14 @@ def Test_browse()
CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 4') CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 4')
enddef enddef
def Test_bufexists()
assert_fails('bufexists(true)', 'E1174')
enddef
def Test_buflisted() def Test_buflisted()
var res: bool = buflisted('asdf') var res: bool = buflisted('asdf')
assert_equal(false, res) assert_equal(false, res)
assert_fails('buflisted(true)', 'E1174')
enddef enddef
def Test_bufname() def Test_bufname()
@ -176,6 +194,8 @@ def Test_bufwinid()
only only
bwipe SomeFile bwipe SomeFile
bwipe OtherFile bwipe OtherFile
assert_fails('bufwinid(true)', 'E1138')
enddef enddef
def Test_call_call() def Test_call_call()

View File

@ -344,12 +344,12 @@ tv_get_float(typval_T *varp)
* Give an error and return FAIL unless "tv" is a string. * Give an error and return FAIL unless "tv" is a string.
*/ */
int int
check_for_string(typval_T *tv, int arg) check_for_string_arg(typval_T *args, int idx)
{ {
if (tv->v_type != VAR_STRING) if (args[idx].v_type != VAR_STRING)
{ {
if (arg > 0) if (idx >= 0)
semsg(_(e_string_required_for_argument_nr), arg); semsg(_(e_string_required_for_argument_nr), idx + 1);
else else
emsg(_(e_stringreq)); emsg(_(e_stringreq));
return FAIL; return FAIL;
@ -358,17 +358,17 @@ check_for_string(typval_T *tv, int arg)
} }
/* /*
* Give an error and return FAIL unless "tv" is a non-empty string. * Give an error and return FAIL unless "args[idx]" is a non-empty string.
*/ */
int int
check_for_nonempty_string(typval_T *tv, int arg) check_for_nonempty_string_arg(typval_T *args, int idx)
{ {
if (check_for_string(tv, arg) == FAIL) if (check_for_string_arg(args, idx) == FAIL)
return FAIL; return FAIL;
if (tv->vval.v_string == NULL || *tv->vval.v_string == NUL) if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
{ {
if (arg > 0) if (idx >= 0)
semsg(_(e_non_empty_string_required_for_argument_nr), arg); semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
else else
emsg(_(e_non_empty_string_required)); emsg(_(e_non_empty_string_required));
return FAIL; return FAIL;

View File

@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
2664,
/**/ /**/
2663, 2663,
/**/ /**/