Patch 8.0.0642
Problem: writefile() continues after detecting an error.
Solution: Bail out as soon as an error is detected. (suggestions by Nikolai
Pavlov, closes #1476)
Files: src/evalfunc.c, src/testdir/test_writefile.vim
*** ../vim-8.0.0641/src/evalfunc.c 2017-06-05 18:46:20.497845949 +0200
--- src/evalfunc.c 2017-06-13 19:34:00.767540909 +0200
***************
*** 13179,13185 ****
--- 13179,13188 ----
char_u *fname;
FILE *fd;
int ret = 0;
+ listitem_T *li;
+ list_T *list;
+ rettv->vval.v_number = -1;
if (check_restricted() || check_secure())
return;
***************
*** 13188,13207 ****
EMSG2(_(e_listarg), "writefile()");
return;
}
! if (argvars[0].vval.v_list == NULL)
return;
if (argvars[2].v_type != VAR_UNKNOWN)
{
! if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
binary = TRUE;
! if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
append = TRUE;
}
/* Always open the file in binary mode, library functions have a mind of
* their own about CR-LF conversion. */
- fname = get_tv_string(&argvars[1]);
if (*fname == NUL || (fd = mch_fopen((char *)fname,
append ? APPENDBIN : WRITEBIN)) == NULL)
{
--- 13191,13221 ----
EMSG2(_(e_listarg), "writefile()");
return;
}
! list = argvars[0].vval.v_list;
! if (list == NULL)
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)
{
! char_u *arg2 = get_tv_string_chk(&argvars[2]);
!
! if (arg2 == NULL)
! return;
! if (vim_strchr(arg2, 'b') != NULL)
binary = TRUE;
! if (vim_strchr(arg2, 'a') != NULL)
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
* their own about CR-LF conversion. */
if (*fname == NUL || (fd = mch_fopen((char *)fname,
append ? APPENDBIN : WRITEBIN)) == NULL)
{
***************
*** 13210,13216 ****
}
else
{
! if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
ret = -1;
fclose(fd);
}
--- 13224,13230 ----
}
else
{
! if (write_list(fd, list, binary) == FAIL)
ret = -1;
fclose(fd);
}
*** ../vim-8.0.0641/src/testdir/test_writefile.vim 2016-09-01
22:07:07.000000000 +0200
--- src/testdir/test_writefile.vim 2017-06-13 19:28:49.369538266 +0200
***************
*** 1,5 ****
! function! Test_WriteFile()
let f = tempname()
call writefile(["over","written"], f, "b")
call writefile(["hello","world"], f, "b")
--- 1,6 ----
+ " Tests for the writefile() function.
! func Test_writefile()
let f = tempname()
call writefile(["over","written"], f, "b")
call writefile(["hello","world"], f, "b")
***************
*** 13,16 ****
call assert_equal("morning", l[3])
call assert_equal("vimmers", l[4])
call delete(f)
! endfunction
--- 14,33 ----
call assert_equal("morning", l[3])
call assert_equal("vimmers", l[4])
call delete(f)
! 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
*** ../vim-8.0.0641/src/version.c 2017-06-13 17:20:35.691782326 +0200
--- src/version.c 2017-06-13 19:23:42.763610095 +0200
***************
*** 766,767 ****
--- 766,769 ----
{ /* Add new patch number below this line */
+ /**/
+ 642,
/**/
--
hundred-and-one symptoms of being an internet addict:
33. You name your children Eudora, Mozilla and Dotcom.
/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.