eval: Make sure that v:_null_dict does not crash dictwatcher*()

Ref #4615
This commit is contained in:
ZyX 2016-12-04 22:59:25 +03:00
parent a56f2d27e3
commit 3025431c81
2 changed files with 27 additions and 0 deletions

View File

@ -808,6 +808,9 @@ void tv_dict_watcher_add(dict_T *const dict, const char *const key_pattern,
const size_t key_pattern_len, Callback callback)
FUNC_ATTR_NONNULL_ARG(2)
{
if (dict == NULL) {
return;
}
DictWatcher *const watcher = xmalloc(sizeof(DictWatcher));
watcher->key_pattern = xmemdupz(key_pattern, key_pattern_len);
watcher->key_pattern_len = key_pattern_len;

View File

@ -244,6 +244,16 @@ describe('dictionary change notifications', function()
end)
end)
it('errors out when adding to v:_null_dict', function()
command([[
function! g:Watcher1(dict, key, value)
call rpcnotify(g:channel, '1', a:key, a:value)
endfunction
]])
eq('Vim(call):E46: Cannot change read-only variable "dictwatcheradd() argument"',
exc_exec('call dictwatcheradd(v:_null_dict, "x", "g:Watcher1")'))
end)
describe('errors', function()
before_each(function()
source([[
@ -272,6 +282,20 @@ describe('dictionary change notifications', function()
command('call dictwatcherdel(g:, "key", "g:InvalidCb")')
end)
it('fails to remove watcher from v:_null_dict', function()
eq("Vim(call):Couldn't find a watcher matching key and callback",
exc_exec('call dictwatcherdel(v:_null_dict, "x", "g:Watcher2")'))
end)
--[[
[ it("fails to add/remove if the callback doesn't exist", function()
[ eq("Vim(call):Function g:InvalidCb doesn't exist",
[ exc_exec('call dictwatcheradd(g:, "key", "g:InvalidCb")'))
[ eq("Vim(call):Function g:InvalidCb doesn't exist",
[ exc_exec('call dictwatcherdel(g:, "key", "g:InvalidCb")'))
[ end)
]]
it('does not fail to replace a watcher function', function()
source([[
let g:key = 'v2'