changeset 23142:5f08d4a42898 v8.2.2117

patch 8.2.2117: some functions use any value as a string Commit: https://github.com/vim/vim/commit/7bb4e74c38642682cfdd0cb4052adfa5efdd7dd1 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Dec 9 12:41:50 2020 +0100 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.
author Bram Moolenaar <Bram@vim.org>
date Wed, 09 Dec 2020 12:45:05 +0100
parents a166b1a0cb41
children 6028d0f8f533
files src/filepath.c src/mbyte.c src/proto/typval.pro src/testdir/test_vim9_builtin.vim src/testdir/test_vim9_expr.vim src/typval.c src/version.c
diffstat 7 files changed, 113 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/filepath.c
+++ b/src/filepath.c
@@ -861,10 +861,11 @@ f_delete(typval_T *argvars, typval_T *re
     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 *r
 {
     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
     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
     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_
     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 *
     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 *
     char_u	*fname;
     stat_T	st;
 
+    if (in_vim9script() && check_for_string(&argvars[0]) == FAIL)
+	return;
+
     fname = tv_get_string(&argvars[0]);
-
-    rettv->v_type = VAR_NUMBER;
-
     if (mch_stat((char *)fname, &st) >= 0)
     {
 	if (mch_isdir(fname))
@@ -1169,8 +1183,9 @@ f_getftime(typval_T *argvars, typval_T *
     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 *
     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;
--- a/src/mbyte.c
+++ b/src/mbyte.c
@@ -5551,13 +5551,8 @@ f_setcellwidths(typval_T *argvars, typva
     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
--- a/src/proto/typval.pro
+++ b/src/proto/typval.pro
@@ -9,6 +9,7 @@ varnumber_T tv_get_number_chk(typval_T *
 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);
--- 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([])
--- 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()
--- 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!
--- 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,