From 4dea2d92e4c308b064ed1dd7b2ba7527a0d6b0a0 Mon Sep 17 00:00:00 2001 From: Bram Moolenaar Date: Thu, 31 Mar 2022 11:37:57 +0100 Subject: 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) --- src/filepath.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'src/filepath.c') diff --git a/src/filepath.c b/src/filepath.c index f0da60f452..851091e57a 100644 --- a/src/filepath.c +++ b/src/filepath.c @@ -893,32 +893,34 @@ f_exepath(typval_T *argvars, typval_T *rettv) } /* - * "filereadable()" function + * Return TRUE if "fname" is a readable file. */ - void -f_filereadable(typval_T *argvars, typval_T *rettv) + int +file_is_readable(char_u *fname) { 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) + if (*fname && !mch_isdir(fname) + && (fd = mch_open((char *)fname, O_RDONLY | O_NONBLOCK, 0)) >= 0) { - n = TRUE; close(fd); + return TRUE; } - else - n = FALSE; + return FALSE; +} - rettv->vval.v_number = n; +/* + * "filereadable()" function + */ + void +f_filereadable(typval_T *argvars, typval_T *rettv) +{ + if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) + return; + rettv->vval.v_number = file_is_readable(tv_get_string(&argvars[0])); } /* @@ -1761,7 +1763,7 @@ read_file_or_blob(typval_T *argvars, typval_T *rettv, int always_blob) 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) -- cgit v1.2.3