diff src/filepath.c @ 28255:48b9ffd40f38 v8.2.4653

patch 8.2.4653: "import autoload" does not check the file name Commit: https://github.com/vim/vim/commit/4dea2d92e4c308b064ed1dd7b2ba7527a0d6b0a0 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Mar 31 11:37:57 2022 +0100 patch 8.2.4653: "import autoload" does not check the file name Problem: "import autoload" does not check the file name. Solution: Give an error if the file is not readable. (closes https://github.com/vim/vim/issues/10049)
author Bram Moolenaar <Bram@vim.org>
date Thu, 31 Mar 2022 12:45:05 +0200
parents d4b549180a07
children 1c97a31f8f22
line wrap: on
line diff
--- a/src/filepath.c
+++ b/src/filepath.c
@@ -893,32 +893,34 @@ f_exepath(typval_T *argvars, typval_T *r
 }
 
 /*
+ * Return TRUE if "fname" is a readable file.
+ */
+    int
+file_is_readable(char_u *fname)
+{
+    int		fd;
+
+#ifndef O_NONBLOCK
+# define O_NONBLOCK 0
+#endif
+    if (*fname && !mch_isdir(fname)
+	      && (fd = mch_open((char *)fname, O_RDONLY | O_NONBLOCK, 0)) >= 0)
+    {
+	close(fd);
+	return TRUE;
+    }
+    return FALSE;
+}
+
+/*
  * "filereadable()" function
  */
     void
 f_filereadable(typval_T *argvars, typval_T *rettv)
 {
-    int		fd;
-    char_u	*p;
-    int		n;
-
     if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
 	return;
-
-#ifndef O_NONBLOCK
-# define O_NONBLOCK 0
-#endif
-    p = tv_get_string(&argvars[0]);
-    if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
-					      O_RDONLY | O_NONBLOCK, 0)) >= 0)
-    {
-	n = TRUE;
-	close(fd);
-    }
-    else
-	n = FALSE;
-
-    rettv->vval.v_number = n;
+    rettv->vval.v_number = file_is_readable(tv_get_string(&argvars[0]));
 }
 
 /*
@@ -1761,7 +1763,7 @@ read_file_or_blob(typval_T *argvars, typ
 
     if (mch_isdir(fname))
     {
-	semsg(_(e_src_is_directory), fname);
+	semsg(_(e_str_is_directory), fname);
 	return;
     }
     if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)