Skip to content

Commit

Permalink
eval: Make sure that v:_null_dict does not crash dictwatcher*()
Browse files Browse the repository at this point in the history
  • Loading branch information
ZyX-I committed Dec 8, 2016
1 parent 0d54f0d commit 579ed97
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/nvim/eval/typval.c
Expand Up @@ -791,6 +791,9 @@ void tv_dict_watcher_add(dict_T *const dict, const char *const key_pattern,
const size_t key_pattern_len, ufunc_T *const callback)
FUNC_ATTR_NONNULL_ARG(2, 4)
{
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;
Expand Down
15 changes: 15 additions & 0 deletions test/functional/ex_cmds/dict_notifications_spec.lua
Expand Up @@ -255,6 +255,16 @@ describe('dictionary change notifications', function()
end)
end)

it('silently ignores adds to v:_null_dict', function()
nvim_command([[
function! g:Watcher1(dict, key, value)
call rpcnotify(g:channel, '1', a:key, a:value)
endfunction
]])
eq(0,
exc_exec('call dictwatcheradd(v:_null_dict, "x", "g:Watcher1")'))
end)

describe('errors', function()
before_each(function()
source([[
Expand All @@ -278,6 +288,11 @@ describe('dictionary change notifications', function()
exc_exec('call dictwatcherdel(g:, "invalid_key", "g:Watcher2")'))
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")'))
Expand Down

0 comments on commit 579ed97

Please sign in to comment.