vim-patch:8.2.0521: crash when reading a blob fails

Problem:    Crash when reading a blob fails.
Solution:   Avoid keeping a pointer to a freed blob object. (Dominique Pelle,
            closes vim/vim#5890)  Adjust error messages.
15352dc6ec
This commit is contained in:
Sean Dewar
2020-11-25 16:02:10 +00:00
parent ba34afb378
commit 9b5c9dbfa7
2 changed files with 11 additions and 1 deletions

View File

@@ -7015,6 +7015,11 @@ static void f_readfile(typval_T *argvars, typval_T *rettv, FunPtr fptr)
// Always open the file in binary mode, library functions have a mind of
// their own about CR-LF conversion.
const char *const fname = tv_get_string(&argvars[0]);
if (os_isdir((const char_u *)fname)) {
EMSG2(_(e_isadir2), fname);
return;
}
if (*fname == NUL || (fd = os_fopen(fname, READBIN)) == NULL) {
EMSG2(_(e_notopen), *fname == NUL ? _("<empty>") : fname);
return;
@@ -7023,8 +7028,10 @@ static void f_readfile(typval_T *argvars, typval_T *rettv, FunPtr fptr)
if (blob) {
tv_blob_alloc_ret(rettv);
if (!read_blob(fd, rettv->vval.v_blob)) {
EMSG("cannot read file");
EMSG2(_(e_notread), fname);
// An empty blob is returned on error.
tv_blob_free(rettv->vval.v_blob);
rettv->vval.v_blob = NULL;
}
fclose(fd);
return;

View File

@@ -256,6 +256,9 @@ func Test_blob_read_write()
let br = readfile('Xblob', 'B')
call assert_equal(b, br)
call delete('Xblob')
" This was crashing when calling readfile() with a directory.
call assert_fails("call readfile('.', 'B')", 'E17: "." is a directory')
endfunc
" filter() item in blob