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 #10049)
diff --git a/src/errors.h b/src/errors.h
index 949a532..951acab 100644
--- a/src/errors.h
+++ b/src/errors.h
@@ -30,8 +30,9 @@
#endif
EXTERN char e_invalid_range[]
INIT(= N_("E16: Invalid range"));
-#if defined(UNIX) || defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
-EXTERN char e_src_is_directory[]
+#if defined(UNIX) || defined(FEAT_SYN_HL) \
+ || defined(FEAT_SPELL) || defined(FEAT_EVAL)
+EXTERN char e_str_is_directory[]
INIT(= N_("E17: \"%s\" is a directory"));
#endif
#ifdef FEAT_EVAL
diff --git a/src/ex_cmds.c b/src/ex_cmds.c
index be358453..25f06e6 100644
--- a/src/ex_cmds.c
+++ b/src/ex_cmds.c
@@ -2116,7 +2116,7 @@
// with UNIX it is possible to open a directory
if (mch_isdir(ffname))
{
- semsg(_(e_src_is_directory), ffname);
+ semsg(_(e_str_is_directory), ffname);
return FAIL;
}
#endif
diff --git a/src/ex_docmd.c b/src/ex_docmd.c
index c5e96bd..c12f151 100644
--- a/src/ex_docmd.c
+++ b/src/ex_docmd.c
@@ -8386,7 +8386,7 @@
// with Unix it is possible to open a directory
if (mch_isdir(fname))
{
- semsg(_(e_src_is_directory), fname);
+ semsg(_(e_str_is_directory), fname);
return NULL;
}
#endif
diff --git a/src/filepath.c b/src/filepath.c
index f0da60f..851091e 100644
--- a/src/filepath.c
+++ b/src/filepath.c
@@ -893,32 +893,34 @@
}
/*
+ * 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 @@
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)
diff --git a/src/proto/filepath.pro b/src/proto/filepath.pro
index 6261211..bf3d516 100644
--- a/src/proto/filepath.pro
+++ b/src/proto/filepath.pro
@@ -5,6 +5,7 @@
void f_delete(typval_T *argvars, typval_T *rettv);
void f_executable(typval_T *argvars, typval_T *rettv);
void f_exepath(typval_T *argvars, typval_T *rettv);
+int file_is_readable(char_u *fname);
void f_filereadable(typval_T *argvars, typval_T *rettv);
void f_filewritable(typval_T *argvars, typval_T *rettv);
void f_finddir(typval_T *argvars, typval_T *rettv);
diff --git a/src/spellfile.c b/src/spellfile.c
index ad0d000..aaacb2e 100644
--- a/src/spellfile.c
+++ b/src/spellfile.c
@@ -5976,7 +5976,7 @@
}
if (mch_isdir(wfname))
{
- semsg(_(e_src_is_directory), wfname);
+ semsg(_(e_str_is_directory), wfname);
goto theend;
}
diff --git a/src/testdir/test_vim9_import.vim b/src/testdir/test_vim9_import.vim
index b1e96c5..c6a8206 100644
--- a/src/testdir/test_vim9_import.vim
+++ b/src/testdir/test_vim9_import.vim
@@ -2508,13 +2508,19 @@
vim9script
import autoload './doesNotExist.vim'
END
- v9.CheckScriptSuccess(lines)
+ v9.CheckScriptFailure(lines, 'E282:', 2)
lines =<< trim END
vim9script
import autoload '/dir/doesNotExist.vim'
END
- v9.CheckScriptSuccess(lines)
+ v9.CheckScriptFailure(lines, 'E282:', 2)
+
+ lines =<< trim END
+ vim9script
+ import autoload '../testdir'
+ END
+ v9.CheckScriptFailure(lines, 'E17:', 2)
lines =<< trim END
vim9script
diff --git a/src/version.c b/src/version.c
index 843affd..3db4108 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 4653,
+/**/
4652,
/**/
4651,