Skip to content

Commit 5259275

Browse files
committed
patch 8.2.0507: getbufvar() may get the wrong dictionary
Problem: Getbufvar() may get the wrong dictionary. (David le Blanc) Solution: Check for empty name. (closes #5878)
1 parent d1e9dc2 commit 5259275

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

src/evalvars.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,6 +2382,7 @@ find_var(char_u *name, hashtab_T **htp, int no_autoload)
23822382

23832383
/*
23842384
* Find variable "varname" in hashtab "ht" with name "htname".
2385+
* When "varname" is empty returns curwin/curtab/etc vars dictionary.
23852386
* Returns NULL if not found.
23862387
*/
23872388
dictitem_T *
@@ -3503,8 +3504,12 @@ f_getbufvar(typval_T *argvars, typval_T *rettv)
35033504
else
35043505
{
35053506
// Look up the variable.
3506-
// Let getbufvar({nr}, "") return the "b:" dictionary.
3507-
v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b', varname, FALSE);
3507+
if (*varname == NUL)
3508+
// Let getbufvar({nr}, "") return the "b:" dictionary.
3509+
v = &buf->b_bufvar;
3510+
else
3511+
v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3512+
varname, FALSE);
35083513
if (v != NULL)
35093514
{
35103515
copy_tv(&v->di_tv, rettv);

src/testdir/test_functions.vim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,16 @@ func Test_getbufvar()
841841
call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
842842
close
843843

844+
" Get the b: dict.
845+
let b:testvar = 'one'
846+
new
847+
let b:testvar = 'two'
848+
let thebuf = bufnr()
849+
wincmd w
850+
call assert_equal('two', getbufvar(thebuf, 'testvar'))
851+
call assert_equal('two', getbufvar(thebuf, '').testvar)
852+
bwipe!
853+
844854
set fileformats&
845855
endfunc
846856

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,8 @@ static char *(features[]) =
738738

739739
static int included_patches[] =
740740
{ /* Add new patch number below this line */
741+
/**/
742+
507,
741743
/**/
742744
506,
743745
/**/

0 commit comments

Comments
 (0)