aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/undo.c9
-rw-r--r--test/old/testdir/test_undo.vim12
2 files changed, 18 insertions, 3 deletions
diff --git a/src/nvim/undo.c b/src/nvim/undo.c
index 695cf81f73..e27304f04c 100644
--- a/src/nvim/undo.c
+++ b/src/nvim/undo.c
@@ -3171,11 +3171,14 @@ void f_undofile(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
/// "undotree(expr)" function
void f_undotree(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
- typval_T *const tv = &argvars[0];
- buf_T *const buf = tv->v_type == VAR_UNKNOWN ? curbuf : tv_get_buf_from_arg(tv);
-
tv_dict_alloc_ret(rettv);
+ typval_T *const tv = &argvars[0];
+ buf_T *const buf = tv->v_type == VAR_UNKNOWN ? curbuf : get_buf_arg(tv);
+ if (buf == NULL) {
+ return;
+ }
+
dict_T *dict = rettv->vval.v_dict;
tv_dict_add_nr(dict, S_LEN("synced"), (varnumber_T)buf->b_u_synced);
diff --git a/test/old/testdir/test_undo.vim b/test/old/testdir/test_undo.vim
index 08a0ba4c39..980aa84be6 100644
--- a/test/old/testdir/test_undo.vim
+++ b/test/old/testdir/test_undo.vim
@@ -134,6 +134,18 @@ func Test_undotree_bufnr()
call assert_notequal(d1, d)
call assert_equal(d2, d)
+ " error cases
+ call assert_fails('call undotree(-1)', 'E158:')
+ call assert_fails('call undotree("nosuchbuf")', 'E158:')
+
+ " after creating a buffer nosuchbuf, undotree('nosuchbuf') should
+ " not error out
+ new nosuchbuf
+ let d = {'seq_last': 0, 'entries': [], 'time_cur': 0, 'save_last': 0, 'synced': 1, 'save_cur': 0, 'seq_cur': 0}
+ call assert_equal(d, undotree("nosuchbuf"))
+ " clean up
+ bw nosuchbuf
+
" Drop created windows
set ul&
new