From 6b3db3f92906b1c155b11f177c9378d9ff4d7d6b Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 28 Oct 2022 11:56:29 +0800 Subject: [PATCH] vim-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 vim/vim#8302) https://github.com/vim/vim/commit/6f1d2aa437744a7cb0206fdaa543a788c5a56c79 Co-authored-by: Bram Moolenaar --- src/nvim/eval.c | 6 +----- src/nvim/eval/typval.c | 18 ++++++++++++++++++ src/nvim/testdir/test_functions.vim | 5 +++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 5b5b4d259c..72df343932 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -1760,11 +1760,7 @@ void set_var_lval(lval_T *lp, char *endp, typval_T *rettv, int copy, const bool semsg(_(e_dictkey), lp->ll_newkey); return; } - if ((lp->ll_tv->vval.v_dict == &globvardict - // || lp->ll_tv->vval.v_dict == &SCRIPT_ITEM(current_sctx.sc_sid)->sn_vars->sv_dict - ) - && (rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL) - && var_wrong_func_name(lp->ll_newkey, true)) { + if (tv_dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv, lp->ll_newkey)) { return; } diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index 31b73f8d48..2b44dacca4 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -2206,6 +2206,18 @@ bool tv_dict_get_callback(dict_T *const d, const char *const key, const ptrdiff_ return res; } +/// Check for adding a function to g: or s:. +/// If the name is wrong give an error message and return true. +int tv_dict_wrong_func_name(dict_T *d, typval_T *tv, const char *name) +{ + return (d == &globvardict + // || (SCRIPT_ID_VALID(current_sctx.sc_sid) + // && d == &SCRIPT_ITEM(current_sctx.sc_sid)->sn_vars->sv_dict) + ) + && (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL) + && var_wrong_func_name(name, true); +} + //{{{2 dict_add* /// Add item to dictionary @@ -2217,6 +2229,9 @@ bool tv_dict_get_callback(dict_T *const d, const char *const key, const ptrdiff_ int tv_dict_add(dict_T *const d, dictitem_T *const item) FUNC_ATTR_NONNULL_ALL { + if (tv_dict_wrong_func_name(d, &item->di_tv, (const char *)item->di_key)) { + return FAIL; + } return hash_add(&d->dv_hashtab, item->di_key); } @@ -2477,6 +2492,9 @@ void tv_dict_extend(dict_T *const d1, dict_T *const d2, const char *const action || var_check_ro(di1->di_flags, arg_errmsg, arg_errmsg_len)) { break; } + if (tv_dict_wrong_func_name(d1, &di2->di_tv, (const char *)di2->di_key)) { + break; + } if (watched) { tv_copy(&di1->di_tv, &oldtv); diff --git a/src/nvim/testdir/test_functions.vim b/src/nvim/testdir/test_functions.vim index afeea20f2d..011e9e000e 100644 --- a/src/nvim/testdir/test_functions.vim +++ b/src/nvim/testdir/test_functions.vim @@ -2488,6 +2488,11 @@ func Test_builtin_check() call assert_fails('let g:.trim = {x -> " " .. x}', 'E704:') call assert_fails('let s:["trim"] = {x -> " " .. x}', 'E704:') call assert_fails('let s:.trim = {x -> " " .. x}', 'E704:') + + call assert_fails('call extend(g:, #{foo: { -> "foo" }})', 'E704:') + let g:bar = 123 + call extend(g:, #{bar: { -> "foo" }}, "keep") + call assert_fails('call extend(g:, #{bar: { -> "foo" }}, "force")', 'E704:') endfunc