]> granicus.if.org Git - vim/commitdiff
patch 8.2.4070: using uninitialized memory when reading empty file v8.2.4070
authorDominique Pelle <dominique.pelle@gmail.com>
Wed, 12 Jan 2022 15:24:40 +0000 (15:24 +0000)
committerBram Moolenaar <Bram@vim.org>
Wed, 12 Jan 2022 15:24:40 +0000 (15:24 +0000)
Problem:    Using uninitialized memory when reading empty file.
Solution:   Check for empty file before checking for NL. (Dominique PellĂ©,
            closes #9511)

src/filepath.c
src/testdir/test_eval_stuff.vim
src/version.c

index eb5b8e050295b63e0adda74cdc6f535f2b980be8..add74b47076949bc400ceb115552d7772466c81e 100644 (file)
@@ -1796,7 +1796,7 @@ read_file_or_blob(typval_T *argvars, typval_T *rettv, int always_blob)
                p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
                ++p)
        {
-           if (*p == '\n' || readlen <= 0)
+           if (readlen <= 0 || *p == '\n')
            {
                listitem_T  *li;
                char_u      *s  = NULL;
index ac9589d24caa5dee3c44a373134e14ce266c9193..ff6d695f62241921b83dd78c8aa1b559e8c20f29 100644 (file)
@@ -93,6 +93,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'))
index a931516a9a60cbd5c8e8ab0a764f325b55b98bfc..8c77e2efb0728dab66b1ec5ca97ce47a61a887ab 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4070,
 /**/
     4069,
 /**/