0
0
mirror of https://github.com/vim/vim.git synced 2025-09-24 03:44:06 -04:00

patch 8.2.2117: some functions use any value as a string

Problem:    Some functions use any value as a string.
Solution:   Check that the value is a non-empty string.
This commit is contained in:
Bram Moolenaar
2020-12-09 12:41:50 +01:00
parent 2c78a772fd
commit 7bb4e74c38
7 changed files with 113 additions and 13 deletions

View File

@@ -861,10 +861,11 @@ 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)
{ {
char_u *name = tv_get_string(&argvars[0]); if (in_vim9script() && check_for_string(&argvars[0]) == FAIL)
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.
rettv->vval.v_number = mch_can_exe(name, NULL, TRUE); rettv->vval.v_number = mch_can_exe(tv_get_string(&argvars[0]), NULL, TRUE);
} }
/* /*
@@ -875,6 +876,8 @@ f_exepath(typval_T *argvars, typval_T *rettv)
{ {
char_u *p = NULL; char_u *p = NULL;
if (in_vim9script() && check_for_string(&argvars[0]) == FAIL)
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;
rettv->vval.v_string = p; rettv->vval.v_string = p;
@@ -890,6 +893,8 @@ 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]) == FAIL)
return;
#ifndef O_NONBLOCK #ifndef O_NONBLOCK
# define O_NONBLOCK 0 # define O_NONBLOCK 0
#endif #endif
@@ -913,6 +918,8 @@ 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]) == FAIL)
return;
rettv->vval.v_number = filewritable(tv_get_string(&argvars[0])); rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
} }
@@ -935,6 +942,8 @@ 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_string(&argvars[0]) == FAIL)
return;
#ifdef FEAT_SEARCHPATH #ifdef FEAT_SEARCHPATH
fname = tv_get_string(&argvars[0]); fname = tv_get_string(&argvars[0]);
@@ -1014,6 +1023,9 @@ 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]) == FAIL
|| check_for_string(&argvars[1]) == FAIL))
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);
if (fname == NULL || mods == NULL) if (fname == NULL || mods == NULL)
@@ -1122,6 +1134,8 @@ 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]) == FAIL)
return;
fname = tv_get_string(&argvars[0]); fname = tv_get_string(&argvars[0]);
rettv->v_type = VAR_STRING; rettv->v_type = VAR_STRING;
@@ -1139,10 +1153,10 @@ 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]) == FAIL)
return;
fname = tv_get_string(&argvars[0]); fname = tv_get_string(&argvars[0]);
rettv->v_type = VAR_NUMBER;
if (mch_stat((char *)fname, &st) >= 0) if (mch_stat((char *)fname, &st) >= 0)
{ {
if (mch_isdir(fname)) if (mch_isdir(fname))
@@ -1169,8 +1183,9 @@ 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]) == FAIL)
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)
rettv->vval.v_number = (varnumber_T)st.st_mtime; rettv->vval.v_number = (varnumber_T)st.st_mtime;
else else
@@ -1214,6 +1229,8 @@ 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]) == FAIL)
return;
fname = tv_get_string(&argvars[0]); fname = tv_get_string(&argvars[0]);
rettv->v_type = VAR_STRING; rettv->v_type = VAR_STRING;

View File

@@ -5551,13 +5551,8 @@ 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 (argvars[0].v_type != VAR_STRING if (check_for_string(&argvars[0]) == FAIL)
|| argvars[0].vval.v_string == NULL
|| *argvars[0].vval.v_string == NUL)
{
emsg(_(e_stringreq));
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);
} }
#endif #endif

View File

@@ -9,6 +9,7 @@ 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);
char_u *tv_get_string(typval_T *varp); char_u *tv_get_string(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);
char_u *tv_get_string_chk(typval_T *varp); char_u *tv_get_string_chk(typval_T *varp);

View File

@@ -185,6 +185,18 @@ def Test_count()
count('ABC ABC ABC', 'b', false)->assert_equal(0) count('ABC ABC ABC', 'b', false)->assert_equal(0)
enddef enddef
def Test_executable()
CheckDefExecFailure(['echo executable(true)'], 'E928:')
CheckDefExecFailure(['echo executable(v:null)'], 'E928:')
CheckDefExecFailure(['echo executable("")'], 'E928:')
enddef
def Test_exepath()
CheckDefExecFailure(['echo exepath(true)'], 'E928:')
CheckDefExecFailure(['echo exepath(v:null)'], 'E928:')
CheckDefExecFailure(['echo exepath("")'], 'E928:')
enddef
def Test_expand() def Test_expand()
split SomeFile split SomeFile
expand('%', true, true)->assert_equal(['SomeFile']) expand('%', true, true)->assert_equal(['SomeFile'])
@@ -241,6 +253,39 @@ def Test_map_function_arg()
CheckDefAndScriptSuccess(lines) CheckDefAndScriptSuccess(lines)
enddef enddef
def Test_filereadable()
CheckDefExecFailure(['echo filereadable(true)'], 'E928:')
CheckDefExecFailure(['echo filereadable(v:null)'], 'E928:')
CheckDefExecFailure(['echo filereadable("")'], 'E928:')
enddef
def Test_filewritable()
CheckDefExecFailure(['echo filewritable(true)'], 'E928:')
CheckDefExecFailure(['echo filewritable(v:null)'], 'E928:')
CheckDefExecFailure(['echo filewritable("")'], 'E928:')
enddef
def Test_finddir()
CheckDefExecFailure(['echo finddir(true)'], 'E928:')
CheckDefExecFailure(['echo finddir(v:null)'], 'E928:')
CheckDefExecFailure(['echo finddir("")'], 'E928:')
enddef
def Test_findfile()
CheckDefExecFailure(['echo findfile(true)'], 'E928:')
CheckDefExecFailure(['echo findfile(v:null)'], 'E928:')
CheckDefExecFailure(['echo findfile("")'], 'E928:')
enddef
def Test_fnamemodify()
CheckDefExecFailure(['echo fnamemodify(true, ":p")'], 'E928:')
CheckDefExecFailure(['echo fnamemodify(v:null, ":p")'], 'E928:')
CheckDefExecFailure(['echo fnamemodify("", ":p")'], 'E928:')
CheckDefExecFailure(['echo fnamemodify("file", true)'], 'E928:')
CheckDefExecFailure(['echo fnamemodify("file", v:null)'], 'E928:')
CheckDefExecFailure(['echo fnamemodify("file", "")'], 'E928:')
enddef
def Test_filter_wrong_dict_key_type() def Test_filter_wrong_dict_key_type()
assert_fails('Wrong_dict_key_type([1, 2, 3])', 'E1012:') assert_fails('Wrong_dict_key_type([1, 2, 3])', 'E1012:')
enddef enddef
@@ -313,6 +358,30 @@ def Test_getloclist_return_type()
d->assert_equal({items: []}) d->assert_equal({items: []})
enddef enddef
def Test_getfperm()
CheckDefExecFailure(['echo getfperm(true)'], 'E928:')
CheckDefExecFailure(['echo getfperm(v:null)'], 'E928:')
CheckDefExecFailure(['echo getfperm("")'], 'E928:')
enddef
def Test_getfsize()
CheckDefExecFailure(['echo getfsize(true)'], 'E928:')
CheckDefExecFailure(['echo getfsize(v:null)'], 'E928:')
CheckDefExecFailure(['echo getfsize("")'], 'E928:')
enddef
def Test_getftime()
CheckDefExecFailure(['echo getftime(true)'], 'E928:')
CheckDefExecFailure(['echo getftime(v:null)'], 'E928:')
CheckDefExecFailure(['echo getftime("")'], 'E928:')
enddef
def Test_getftype()
CheckDefExecFailure(['echo getftype(true)'], 'E928:')
CheckDefExecFailure(['echo getftype(v:null)'], 'E928:')
CheckDefExecFailure(['echo getftype("")'], 'E928:')
enddef
def Test_getqflist_return_type() def Test_getqflist_return_type()
var l = getqflist() var l = getqflist()
l->assert_equal([]) l->assert_equal([])

View File

@@ -1971,7 +1971,7 @@ def Test_expr7_dict()
CheckDefExecFailure(['var x: dict<string> = {a: "x", b: 134}'], 'E1012:', 1) CheckDefExecFailure(['var x: dict<string> = {a: "x", b: 134}'], 'E1012:', 1)
CheckDefFailure(['var x = ({'], 'E723:', 2) CheckDefFailure(['var x = ({'], 'E723:', 2)
CheckDefExecFailure(['{}[getftype("")]'], 'E716: Key not present in Dictionary: ""', 1) CheckDefExecFailure(['{}[getftype("file")]'], 'E716: Key not present in Dictionary: ""', 1)
enddef enddef
def Test_expr7_dict_vim9script() def Test_expr7_dict_vim9script()

View File

@@ -340,6 +340,22 @@ tv_get_float(typval_T *varp)
} }
#endif #endif
/*
* Give an error and return FAIL unless "tv" is a non-empty string.
*/
int
check_for_string(typval_T *tv)
{
if (tv->v_type != VAR_STRING
|| tv->vval.v_string == NULL
|| *tv->vval.v_string == NUL)
{
emsg(_(e_stringreq));
return FAIL;
}
return OK;
}
/* /*
* Get the string value of a variable. * Get the string value of a variable.
* If it is a Number variable, the number is converted into a string. * If it is a Number variable, the number is converted into a string.

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 */
/**/
2117,
/**/ /**/
2116, 2116,
/**/ /**/