forked from aniani/vim
patch 8.0.0642: writefile() continues after detecting an error
Problem: writefile() continues after detecting an error. Solution: Bail out as soon as an error is detected. (suggestions by Nikolai Pavlov, closes #1476)
This commit is contained in:
@@ -13179,7 +13179,10 @@ f_writefile(typval_T *argvars, typval_T *rettv)
|
|||||||
char_u *fname;
|
char_u *fname;
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
listitem_T *li;
|
||||||
|
list_T *list;
|
||||||
|
|
||||||
|
rettv->vval.v_number = -1;
|
||||||
if (check_restricted() || check_secure())
|
if (check_restricted() || check_secure())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -13188,20 +13191,31 @@ f_writefile(typval_T *argvars, typval_T *rettv)
|
|||||||
EMSG2(_(e_listarg), "writefile()");
|
EMSG2(_(e_listarg), "writefile()");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (argvars[0].vval.v_list == NULL)
|
list = argvars[0].vval.v_list;
|
||||||
|
if (list == NULL)
|
||||||
return;
|
return;
|
||||||
|
for (li = list->lv_first; li != NULL; li = li->li_next)
|
||||||
|
if (get_tv_string_chk(&li->li_tv) == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
if (argvars[2].v_type != VAR_UNKNOWN)
|
if (argvars[2].v_type != VAR_UNKNOWN)
|
||||||
{
|
{
|
||||||
if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
|
char_u *arg2 = get_tv_string_chk(&argvars[2]);
|
||||||
|
|
||||||
|
if (arg2 == NULL)
|
||||||
|
return;
|
||||||
|
if (vim_strchr(arg2, 'b') != NULL)
|
||||||
binary = TRUE;
|
binary = TRUE;
|
||||||
if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
|
if (vim_strchr(arg2, 'a') != NULL)
|
||||||
append = TRUE;
|
append = TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fname = get_tv_string_chk(&argvars[1]);
|
||||||
|
if (fname == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
/* Always open the file in binary mode, library functions have a mind of
|
/* Always open the file in binary mode, library functions have a mind of
|
||||||
* their own about CR-LF conversion. */
|
* their own about CR-LF conversion. */
|
||||||
fname = get_tv_string(&argvars[1]);
|
|
||||||
if (*fname == NUL || (fd = mch_fopen((char *)fname,
|
if (*fname == NUL || (fd = mch_fopen((char *)fname,
|
||||||
append ? APPENDBIN : WRITEBIN)) == NULL)
|
append ? APPENDBIN : WRITEBIN)) == NULL)
|
||||||
{
|
{
|
||||||
@@ -13210,7 +13224,7 @@ f_writefile(typval_T *argvars, typval_T *rettv)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
|
if (write_list(fd, list, binary) == FAIL)
|
||||||
ret = -1;
|
ret = -1;
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
|
" Tests for the writefile() function.
|
||||||
|
|
||||||
function! Test_WriteFile()
|
func Test_writefile()
|
||||||
let f = tempname()
|
let f = tempname()
|
||||||
call writefile(["over","written"], f, "b")
|
call writefile(["over","written"], f, "b")
|
||||||
call writefile(["hello","world"], f, "b")
|
call writefile(["hello","world"], f, "b")
|
||||||
@@ -13,4 +14,20 @@ function! Test_WriteFile()
|
|||||||
call assert_equal("morning", l[3])
|
call assert_equal("morning", l[3])
|
||||||
call assert_equal("vimmers", l[4])
|
call assert_equal("vimmers", l[4])
|
||||||
call delete(f)
|
call delete(f)
|
||||||
endfunction
|
endfunc
|
||||||
|
|
||||||
|
func Test_writefile_fails_gently()
|
||||||
|
call assert_fails('call writefile(["test"], "Xfile", [])', 'E730:')
|
||||||
|
call assert_false(filereadable("Xfile"))
|
||||||
|
call delete("Xfile")
|
||||||
|
|
||||||
|
call assert_fails('call writefile(["test", [], [], [], "tset"], "Xfile")', 'E730:')
|
||||||
|
call assert_false(filereadable("Xfile"))
|
||||||
|
call delete("Xfile")
|
||||||
|
|
||||||
|
call assert_fails('call writefile([], "Xfile", [])', 'E730:')
|
||||||
|
call assert_false(filereadable("Xfile"))
|
||||||
|
call delete("Xfile")
|
||||||
|
|
||||||
|
call assert_fails('call writefile([], [])', 'E730:')
|
||||||
|
endfunc
|
||||||
|
@@ -764,6 +764,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
642,
|
||||||
/**/
|
/**/
|
||||||
641,
|
641,
|
||||||
/**/
|
/**/
|
||||||
|
Reference in New Issue
Block a user