aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZyX <kp-pav@yandex.ru>2016-12-04 22:59:25 +0300
committerZyX <kp-pav@yandex.ru>2017-03-29 10:08:44 +0300
commit3025431c81daac873e69a71aee695ebfd00504f7 (patch)
treeb695ebfa8c1b93ea2d3debafe2c985d9054132bb
parenta56f2d27e3c09aaae00a58a70652ac5db3287dee (diff)
downloadrneovim-3025431c81daac873e69a71aee695ebfd00504f7.tar.gz
rneovim-3025431c81daac873e69a71aee695ebfd00504f7.tar.bz2
rneovim-3025431c81daac873e69a71aee695ebfd00504f7.zip
eval: Make sure that v:_null_dict does not crash dictwatcher*()
Ref #4615
-rw-r--r--src/nvim/eval/typval.c3
-rw-r--r--test/functional/ex_cmds/dict_notifications_spec.lua24
2 files changed, 27 insertions, 0 deletions
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c
index 243e738c50..8c11f4bd8f 100644
--- a/src/nvim/eval/typval.c
+++ b/src/nvim/eval/typval.c
@@ -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;
diff --git a/test/functional/ex_cmds/dict_notifications_spec.lua b/test/functional/ex_cmds/dict_notifications_spec.lua
index 8cc717483e..e3b4a1c504 100644
--- a/test/functional/ex_cmds/dict_notifications_spec.lua
+++ b/test/functional/ex_cmds/dict_notifications_spec.lua
@@ -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'