From cca6c4c6986abc67cd213ad1d32d329384a57790 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Wed, 6 Dec 2023 17:02:13 +0100 Subject: feat(rpc): allow empty string key in msgpack => Vim conversion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: Since e057b38e7037 #20757 we support empty key in JSON encode/decode, but we don't allow it in RPC object => Vim dict conversion. But empty string is a valid key in Vim dicts and the msgpack spec. Empty string key was disallowed in 7c01d5ff9286 (2014) but that commit/PR doesn't explicitly discuss it, so presumably it was a "seems reasonable" decision (or Vimscript didn't allow empty keys until later). Solution: Remove the check in `object_to_vim()`. Note that `tv_dict_item_alloc_len` will invoke `memcpy(…, 0)` but that's allowed by the C spec: https://stackoverflow.com/a/3751937/152142 --- test/functional/api/vim_spec.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'test/functional/api/vim_spec.lua') diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 7287666190..abae346701 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -1319,6 +1319,10 @@ describe('API', function() eq("Key not found: lua", pcall_err(meths.del_var, 'lua')) meths.set_var('lua', 1) + -- Empty keys are allowed in Vim dicts (and msgpack). + nvim('set_var', 'dict_empty_key', {[''] = 'empty key'}) + eq({[''] = 'empty key'}, nvim('get_var', 'dict_empty_key')) + -- Set locked g: var. command('lockvar lua') eq('Key is locked: lua', pcall_err(meths.del_var, 'lua')) @@ -1983,7 +1987,7 @@ describe('API', function() it('errors when context dictionary is invalid', function() eq('E474: Failed to convert list to msgpack string buffer', pcall_err(nvim, 'load_context', { regs = { {} }, jumps = { {} } })) - eq("Empty dictionary keys aren't allowed", + eq('E474: Failed to convert list to msgpack string buffer', pcall_err(nvim, 'load_context', { regs = { { [''] = '' } } })) end) end) -- cgit