vim-patch:8.2.4070: using uninitialized memory when reading empty file

Problem:    Using uninitialized memory when reading empty file.
Solution:   Check for empty file before checking for NL. (Dominique Pellé,
            closes vim/vim#9511)

f5d639a8af

Co-authored-by: Dominique Pelle <dominique.pelle@gmail.com>
This commit is contained in:
zeertzjq 2022-10-27 11:43:10 +08:00
parent 7765f2bb83
commit b793395019
2 changed files with 8 additions and 1 deletions

View File

@ -5921,7 +5921,7 @@ static void f_readfile(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
for (p = buf, start = buf; for (p = buf, start = buf;
p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary)); p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
p++) { p++) {
if (*p == '\n' || readlen <= 0) { if (readlen <= 0 || *p == '\n') {
char *s = NULL; char *s = NULL;
size_t len = (size_t)(p - start); size_t len = (size_t)(p - start);

View File

@ -120,6 +120,13 @@ func Test_readfile_binary()
call delete('XReadfile_bin') call delete('XReadfile_bin')
endfunc endfunc
func Test_readfile_binary_empty()
call writefile([], 'Xempty-file')
" This used to compare uninitialized memory in Vim <= 8.2.4065
call assert_equal([''], readfile('Xempty-file', 'b'))
call delete('Xempty-file')
endfunc
func Test_readfile_bom() func Test_readfile_bom()
call writefile(["\ufeffFOO", "FOO\ufeffBAR"], 'XReadfile_bom') call writefile(["\ufeffFOO", "FOO\ufeffBAR"], 'XReadfile_bom')
call assert_equal(['FOO', 'FOOBAR'], readfile('XReadfile_bom')) call assert_equal(['FOO', 'FOOBAR'], readfile('XReadfile_bom'))