summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2020-12-09 12:41:50 +0100
committerBram Moolenaar <Bram@vim.org>2020-12-09 12:41:50 +0100
commit7bb4e74c38642682cfdd0cb4052adfa5efdd7dd1 (patch)
tree87b6644c08d544bae227365d78c1a7c11c611a6d
parent2c78a772fdb5f6a16d16a47e7f218051c4dcb845 (diff)
patch 8.2.2117: some functions use any value as a stringv8.2.2117
Problem: Some functions use any value as a string. Solution: Check that the value is a non-empty string.
-rw-r--r--src/filepath.c29
-rw-r--r--src/mbyte.c7
-rw-r--r--src/proto/typval.pro1
-rw-r--r--src/testdir/test_vim9_builtin.vim69
-rw-r--r--src/testdir/test_vim9_expr.vim2
-rw-r--r--src/typval.c16
-rw-r--r--src/version.c2
7 files changed, 113 insertions, 13 deletions
diff --git a/src/filepath.c b/src/filepath.c
index 7177275250..89fafe3134 100644
--- a/src/filepath.c
+++ b/src/filepath.c
@@ -861,10 +861,11 @@ f_delete(typval_T *argvars, typval_T *rettv)
void
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.
- 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;
+ if (in_vim9script() && check_for_string(&argvars[0]) == FAIL)
+ return;
(void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
rettv->v_type = VAR_STRING;
rettv->vval.v_string = p;
@@ -890,6 +893,8 @@ f_filereadable(typval_T *argvars, typval_T *rettv)
char_u *p;
int n;
+ if (in_vim9script() && check_for_string(&argvars[0]) == FAIL)
+ return;
#ifndef O_NONBLOCK
# define O_NONBLOCK 0
#endif
@@ -913,6 +918,8 @@ f_filereadable(typval_T *argvars, typval_T *rettv)
void
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]));
}
@@ -935,6 +942,8 @@ findfilendir(
rettv->vval.v_string = NULL;
rettv->v_type = VAR_STRING;
+ if (in_vim9script() && check_for_string(&argvars[0]) == FAIL)
+ return;
#ifdef FEAT_SEARCHPATH
fname = tv_get_string(&argvars[0]);
@@ -1014,6 +1023,9 @@ f_fnamemodify(typval_T *argvars, typval_T *rettv)
char_u *fbuf = NULL;
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]);
mods = tv_get_string_buf_chk(&argvars[1], buf);
if (fname == NULL || mods == NULL)
@@ -1122,6 +1134,8 @@ f_getfperm(typval_T *argvars, typval_T *rettv)
char_u *perm = NULL;
char_u permbuf[] = "---------";
+ if (in_vim9script() && check_for_string(&argvars[0]) == FAIL)
+ return;
fname = tv_get_string(&argvars[0]);
rettv->v_type = VAR_STRING;
@@ -1139,10 +1153,10 @@ f_getfsize(typval_T *argvars, typval_T *rettv)
char_u *fname;
stat_T st;
- fname = tv_get_string(&argvars[0]);
-
- rettv->v_type = VAR_NUMBER;
+ if (in_vim9script() && check_for_string(&argvars[0]) == FAIL)
+ return;
+ fname = tv_get_string(&argvars[0]);
if (mch_stat((char *)fname, &st) >= 0)
{
if (mch_isdir(fname))
@@ -1169,8 +1183,9 @@ f_getftime(typval_T *argvars, typval_T *rettv)
char_u *fname;
stat_T st;
+ if (in_vim9script() && check_for_string(&argvars[0]) == FAIL)
+ return;
fname = tv_get_string(&argvars[0]);
-
if (mch_stat((char *)fname, &st) >= 0)
rettv->vval.v_number = (varnumber_T)st.st_mtime;
else
@@ -1214,6 +1229,8 @@ f_getftype(typval_T *argvars, typval_T *rettv)
stat_T st;
char_u *type = NULL;
+ if (in_vim9script() && check_for_string(&argvars[0]) == FAIL)
+ return;
fname = tv_get_string(&argvars[0]);
rettv->v_type = VAR_STRING;
diff --git a/src/mbyte.c b/src/mbyte.c
index 0abb1648fe..a4eff7837f 100644
--- a/src/mbyte.c
+++ b/src/mbyte.c
@@ -5551,13 +5551,8 @@ f_setcellwidths(typval_T *argvars, typval_T *rettv UNUSED)
void
f_charclass(typval_T *argvars, typval_T *rettv UNUSED)
{
- if (argvars[0].v_type != VAR_STRING
- || argvars[0].vval.v_string == NULL
- || *argvars[0].vval.v_string == NUL)
- {
- emsg(_(e_stringreq));
+ if (check_for_string(&argvars[0]) == FAIL)
return;
- }
rettv->vval.v_number = mb_get_class(argvars[0].vval.v_string);
}
#endif
diff --git a/src/proto/typval.pro b/src/proto/typval.pro
index 9c57226da2..eafc0e6909 100644
--- a/src/proto/typval.pro
+++ b/src/proto/typval.pro
@@ -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_chk(typval_T *varp, int *denote);
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_buf(typval_T *varp, char_u *buf);
char_u *tv_get_string_chk(typval_T *varp);
diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim
index 50b711f8db..927a01fe4f 100644
--- a/src/testdir/test_vim9_builtin.vim
+++ b/src/testdir/test_vim9_builtin.vim
@@ -185,6 +185,18 @@ def Test_count()
count('ABC ABC ABC', 'b', false)->assert_equal(0)
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()
split SomeFile
expand('%', true, true)->assert_equal(['SomeFile'])
@@ -241,6 +253,39 @@ def Test_map_function_arg()
CheckDefAndScriptSuccess(lines)
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()
assert_fails('Wrong_dict_key_type([1, 2, 3])', 'E1012:')
enddef
@@ -313,6 +358,30 @@ def Test_getloclist_return_type()
d->assert_equal({items: []})
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()
var l = getqflist()
l->assert_equal([])
diff --git a/src/testdir/test_vim9_expr.vim b/src/testdir/test_vim9_expr.vim
index 10230d54fe..812aabf99a 100644
--- a/src/testdir/test_vim9_expr.vim
+++ b/src/testdir/test_vim9_expr.vim
@@ -1971,7 +1971,7 @@ def Test_expr7_dict()
CheckDefExecFailure(['var x: dict<string> = {a: "x", b: 134}'], 'E1012:', 1)
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
def Test_expr7_dict_vim9script()
diff --git a/src/typval.c b/src/typval.c
index 0e2513aee3..64112e738c 100644
--- a/src/typval.c
+++ b/src/typval.c
@@ -341,6 +341,22 @@ tv_get_float(typval_T *varp)
#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.
* If it is a Number variable, the number is converted into a string.
* tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
diff --git a/src/version.c b/src/version.c
index efb17f3976..21d051e05d 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 2117,
+/**/
2116,
/**/
2115,