From 9b5c9dbfa7074789dd77201ff7893690073e0f0a Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Wed, 25 Nov 2020 16:02:10 +0000 Subject: 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. https://github.com/vim/vim/commit/15352dc6ec43fd50cc3be4f4fd1ad74d5619da20 --- src/nvim/eval/funcs.c | 9 ++++++++- src/nvim/testdir/test_blob.vim | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 6dd7a92b61..2b844af03f 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -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 ? _("") : 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; diff --git a/src/nvim/testdir/test_blob.vim b/src/nvim/testdir/test_blob.vim index 19f1f6bd42..a102a4d6bd 100644 --- a/src/nvim/testdir/test_blob.vim +++ b/src/nvim/testdir/test_blob.vim @@ -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 -- cgit