]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.2117: some functions use any value as a string v8.2.2117
authorBram Moolenaar <Bram@vim.org>
Wed, 9 Dec 2020 11:41:50 +0000 (12:41 +0100)
committerBram Moolenaar <Bram@vim.org>
Wed, 9 Dec 2020 11:41:50 +0000 (12:41 +0100)
Problem:    Some functions use any value as a string.
Solution:   Check that the value is a non-empty string.

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

index 71772752508a5a1c1a6458e8e1728f874f9b20ed..89fafe3134f916988b83967d9b3f99b4e84b5339 100644 (file)
@@ -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;
index 0abb1648fe67040bbda77870cdb5c0f5def40222..a4eff7837f92c736024825afb61c081e78528f6f 100644 (file)
@@ -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
index 9c57226da2c06d8b517331736737d792b3cfbbe5..eafc0e69093afb7c9dc9cc838d38868be986d34a 100644 (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_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);
index 50b711f8db7817ac0f81bf32267d4ea0172586ff..927a01fe4fef6b9c09b0b122eb7f84715bfbf527 100644 (file)
@@ -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([])
index 10230d54fecc736187ad48368e505a3ba0a12c5d..812aabf99ac137691c3be40046a82c5b2ebf9aba 100644 (file)
@@ -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()
index 0e2513aee39d7d2b363f86396a3b15dafd45f5fe..64112e738c324801adf0e9ee5fa6ff91fc49b8a0 100644 (file)
@@ -340,6 +340,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.
index efb17f39766640ee89419f658647f2f34c92c1ba..21d051e05d73eccb378fc86352da1791a1f95a03 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2117,
 /**/
     2116,
 /**/