Mercurial > vim
changeset 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 | 6c31b1126b9f |
children | 7b86c98a5bf2 |
files | src/errors.h src/ex_cmds.c src/ex_docmd.c src/filepath.c src/proto/filepath.pro src/spellfile.c src/testdir/test_vim9_import.vim src/version.c |
diffstat | 8 files changed, 39 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/src/errors.h +++ b/src/errors.h @@ -30,8 +30,9 @@ EXTERN char e_invalid_expression_str[] #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
--- a/src/ex_cmds.c +++ b/src/ex_cmds.c @@ -2116,7 +2116,7 @@ check_overwrite( // 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
--- a/src/ex_docmd.c +++ b/src/ex_docmd.c @@ -8386,7 +8386,7 @@ open_exfile( // 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
--- 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)
--- a/src/proto/filepath.pro +++ b/src/proto/filepath.pro @@ -5,6 +5,7 @@ void f_chdir(typval_T *argvars, typval_T 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);
--- a/src/spellfile.c +++ b/src/spellfile.c @@ -5976,7 +5976,7 @@ mkspell( } if (mch_isdir(wfname)) { - semsg(_(e_src_is_directory), wfname); + semsg(_(e_str_is_directory), wfname); goto theend; }
--- a/src/testdir/test_vim9_import.vim +++ b/src/testdir/test_vim9_import.vim @@ -2508,13 +2508,19 @@ def Test_import_autoload_fails() 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