vim-patch:9.0.0836: wrong error when using extend() with funcref

Problem:    Wrong error when using extend() with funcref.
Solution:   Better check the variable type. (closes vim/vim#11468, closes vim/vim#11455)

91c75d18d9
This commit is contained in:
zeertzjq 2022-11-06 07:01:45 +08:00
parent 4740672b37
commit d4353c3645
3 changed files with 26 additions and 13 deletions

View File

@ -2206,12 +2206,12 @@ bool tv_dict_get_callback(dict_T *const d, const char *const key, const ptrdiff_
return res; return res;
} }
/// Check for adding a function to g: or s:. /// Check for adding a function to g: or l:.
/// If the name is wrong give an error message and return true. /// 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) int tv_dict_wrong_func_name(dict_T *d, typval_T *tv, const char *name)
{ {
return (d == &globvardict || &d->dv_hashtab == get_funccal_local_ht()) return (d == &globvardict || &d->dv_hashtab == get_funccal_local_ht())
&& (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL) && tv_is_func(*tv)
&& var_wrong_func_name(name, true); && var_wrong_func_name(name, true);
} }
@ -2459,17 +2459,9 @@ void tv_dict_extend(dict_T *const d1, dict_T *const d2, const char *const action
TV_DICT_ITER(d2, di2, { TV_DICT_ITER(d2, di2, {
dictitem_T *const di1 = tv_dict_find(d1, (const char *)di2->di_key, -1); dictitem_T *const di1 = tv_dict_find(d1, (const char *)di2->di_key, -1);
if (d1->dv_scope != VAR_NO_SCOPE) { // Check the key to be valid when adding to any scope.
// Disallow replacing a builtin function in l: and g:. if (d1->dv_scope != VAR_NO_SCOPE && !valid_varname((const char *)di2->di_key)) {
// Check the key to be valid when adding to any scope. break;
if (d1->dv_scope == VAR_DEF_SCOPE
&& tv_is_func(di2->di_tv)
&& var_wrong_func_name((const char *)di2->di_key, di1 == NULL)) {
break;
}
if (!valid_varname((const char *)di2->di_key)) {
break;
}
} }
if (di1 == NULL) { if (di1 == NULL) {
dictitem_T *const new_di = tv_dict_item_copy(di2); dictitem_T *const new_di = tv_dict_item_copy(di2);
@ -2489,6 +2481,7 @@ 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)) { || var_check_ro(di1->di_flags, arg_errmsg, arg_errmsg_len)) {
break; break;
} }
// Disallow replacing a builtin function.
if (tv_dict_wrong_func_name(d1, &di2->di_tv, (const char *)di2->di_key)) { if (tv_dict_wrong_func_name(d1, &di2->di_tv, (const char *)di2->di_key)) {
break; break;
} }

View File

@ -2498,6 +2498,25 @@ func Test_builtin_check()
let g:bar = 123 let g:bar = 123
call extend(g:, #{bar: { -> "foo" }}, "keep") call extend(g:, #{bar: { -> "foo" }}, "keep")
call assert_fails('call extend(g:, #{bar: { -> "foo" }}, "force")', 'E704:') call assert_fails('call extend(g:, #{bar: { -> "foo" }}, "force")', 'E704:')
unlet g:bar
call assert_fails('call extend(l:, #{foo: { -> "foo" }})', 'E704:')
let bar = 123
call extend(l:, #{bar: { -> "foo" }}, "keep")
call assert_fails('call extend(l:, #{bar: { -> "foo" }}, "force")', 'E704:')
unlet bar
call assert_fails('call extend(g:, #{foo: function("extend")})', 'E704:')
let g:bar = 123
call extend(g:, #{bar: function("extend")}, "keep")
call assert_fails('call extend(g:, #{bar: function("extend")}, "force")', 'E704:')
unlet g:bar
call assert_fails('call extend(l:, #{foo: function("extend")})', 'E704:')
let bar = 123
call extend(l:, #{bar: function("extend")}, "keep")
call assert_fails('call extend(l:, #{bar: function("extend")}, "force")', 'E704:')
unlet bar
endfunc endfunc

View File

@ -293,6 +293,7 @@ func Test_let_errors()
call assert_fails('let l += 2', 'E734:') call assert_fails('let l += 2', 'E734:')
call assert_fails('let g:["a;b"] = 10', 'E461:') call assert_fails('let g:["a;b"] = 10', 'E461:')
call assert_fails('let g:.min = function("max")', 'E704:') call assert_fails('let g:.min = function("max")', 'E704:')
call assert_fails('let g:cos = "" | let g:.cos = {-> 42}', 'E704:')
if has('channel') if has('channel')
let ch = test_null_channel() let ch = test_null_channel()
call assert_fails('let ch += 1', 'E734:') call assert_fails('let ch += 1', 'E734:')