1
0
forked from aniani/vim

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)
This commit is contained in:
Bram Moolenaar
2022-03-31 11:37:57 +01:00
parent d1d2684c80
commit 4dea2d92e4
8 changed files with 39 additions and 27 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -892,33 +892,35 @@ f_exepath(typval_T *argvars, typval_T *rettv)
rettv->vval.v_string = p;
}
/*
* 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, 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)

View File

@@ -5,6 +5,7 @@ void f_chdir(typval_T *argvars, typval_T *rettv);
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);

View File

@@ -5976,7 +5976,7 @@ mkspell(
}
if (mch_isdir(wfname))
{
semsg(_(e_src_is_directory), wfname);
semsg(_(e_str_is_directory), wfname);
goto theend;
}

View File

@@ -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

View File

@@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
4653,
/**/
4652,
/**/