Skip to content

Commit

Permalink
patch 8.2.4653: "import autoload" does not check the file name
Browse files Browse the repository at this point in the history
Problem:    "import autoload" does not check the file name.
Solution:   Give an error if the file is not readable. (closes #10049)
  • Loading branch information
brammool committed Mar 31, 2022
1 parent d1d2684 commit 4dea2d9
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ex_cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ex_docmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 18 additions & 16 deletions src/filepath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}

/*
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/proto/filepath.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/spellfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5976,7 +5976,7 @@ mkspell(
}
if (mch_isdir(wfname))
{
semsg(_(e_src_is_directory), wfname);
semsg(_(e_str_is_directory), wfname);
goto theend;
}

Expand Down
10 changes: 8 additions & 2 deletions src/testdir/test_vim9_import.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
4653,
/**/
4652,
/**/
Expand Down

0 comments on commit 4dea2d9

Please sign in to comment.