commit b793395019333127e085997b7ced4ea02053697e
parent 7765f2bb8304631c00f1e00ffc73c18cd4d22601
Author: zeertzjq <zeertzjq@outlook.com>
Date: Thu, 27 Oct 2022 11:43:10 +0800
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)
https://github.com/vim/vim/commit/f5d639a8af719eb8ecb141b5c0890627e4d83134
Co-authored-by: Dominique Pelle <dominique.pelle@gmail.com>
Diffstat:
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c
@@ -5921,7 +5921,7 @@ static void f_readfile(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
for (p = buf, start = buf;
p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
p++) {
- if (*p == '\n' || readlen <= 0) {
+ if (readlen <= 0 || *p == '\n') {
char *s = NULL;
size_t len = (size_t)(p - start);
diff --git a/src/nvim/testdir/test_eval_stuff.vim b/src/nvim/testdir/test_eval_stuff.vim
@@ -120,6 +120,13 @@ func Test_readfile_binary()
call delete('XReadfile_bin')
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()
call writefile(["\ufeffFOO", "FOO\ufeffBAR"], 'XReadfile_bom')
call assert_equal(['FOO', 'FOOBAR'], readfile('XReadfile_bom'))