Skip to content

Commit 6f1d2aa

Browse files
committed
patch 8.2.2920: still a way to shadow a builtin function
Problem: Still a way to shadow a builtin function. (Yasuhiro Matsumoto) Solution: Check the key when using extend(). (issue #8302)
1 parent 6a43b37 commit 6f1d2aa

5 files changed

Lines changed: 28 additions & 6 deletions

File tree

src/dict.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,29 @@ dict_copy(dict_T *orig, int deep, int copyID)
344344
return copy;
345345
}
346346

347+
/*
348+
* Check for adding a function to g: or s:.
349+
* If the name is wrong give an error message and return TRUE.
350+
*/
351+
int
352+
dict_wrong_func_name(dict_T *d, typval_T *tv, char_u *name)
353+
{
354+
return (d == get_globvar_dict()
355+
|| (SCRIPT_ID_VALID(current_sctx.sc_sid)
356+
&& d == &SCRIPT_ITEM(current_sctx.sc_sid)->sn_vars->sv_dict))
357+
&& (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
358+
&& var_wrong_func_name(name, TRUE);
359+
}
360+
347361
/*
348362
* Add item "item" to Dictionary "d".
349363
* Returns FAIL when out of memory and when key already exists.
350364
*/
351365
int
352366
dict_add(dict_T *d, dictitem_T *item)
353367
{
368+
if (dict_wrong_func_name(d, &item->di_tv, item->di_key))
369+
return FAIL;
354370
return hash_add(&d->dv_hashtab, item->di_key);
355371
}
356372

@@ -1109,6 +1125,8 @@ dict_extend(dict_T *d1, dict_T *d2, char_u *action)
11091125
if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11101126
|| var_check_ro(di1->di_flags, arg_errmsg, TRUE))
11111127
break;
1128+
if (dict_wrong_func_name(d1, &HI2DI(hi2)->di_tv, hi2->hi_key))
1129+
break;
11121130
clear_tv(&di1->di_tv);
11131131
copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11141132
}

src/eval.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,12 +1462,8 @@ set_var_lval(
14621462
semsg(_(e_dictkey), lp->ll_newkey);
14631463
return;
14641464
}
1465-
if ((lp->ll_tv->vval.v_dict == get_globvar_dict()
1466-
|| lp->ll_tv->vval.v_dict ==
1467-
&SCRIPT_ITEM(current_sctx.sc_sid)->sn_vars->sv_dict)
1468-
&& (rettv->v_type == VAR_FUNC
1469-
|| rettv->v_type == VAR_PARTIAL)
1470-
&& var_wrong_func_name(lp->ll_newkey, TRUE))
1465+
if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1466+
lp->ll_newkey))
14711467
return;
14721468

14731469
// Need to add an item to the Dictionary.

src/proto/dict.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dictitem_T *dictitem_alloc(char_u *key);
1313
void dictitem_remove(dict_T *dict, dictitem_T *item);
1414
void dictitem_free(dictitem_T *item);
1515
dict_T *dict_copy(dict_T *orig, int deep, int copyID);
16+
int dict_wrong_func_name(dict_T *d, typval_T *tv, char_u *name);
1617
int dict_add(dict_T *d, dictitem_T *item);
1718
int dict_add_number(dict_T *d, char *key, varnumber_T nr);
1819
int dict_add_bool(dict_T *d, char *key, varnumber_T nr);

src/testdir/test_functions.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,6 +2686,11 @@ func Test_builtin_check()
26862686
call assert_fails('let g:.trim = {x -> " " .. x}', 'E704:')
26872687
call assert_fails('let s:["trim"] = {x -> " " .. x}', 'E704:')
26882688
call assert_fails('let s:.trim = {x -> " " .. x}', 'E704:')
2689+
2690+
call assert_fails('call extend(g:, #{foo: { -> "foo" }})', 'E704:')
2691+
let g:bar = 123
2692+
call extend(g:, #{bar: { -> "foo" }}, "keep")
2693+
call assert_fails('call extend(g:, #{bar: { -> "foo" }}, "force")', 'E704:')
26892694
endfunc
26902695

26912696

src/version.c

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

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2920,
753755
/**/
754756
2919,
755757
/**/

0 commit comments

Comments
 (0)