diff options
author | zeertzjq <zeertzjq@outlook.com> | 2022-04-29 18:09:25 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-29 18:09:25 +0800 |
commit | 8c548c9e5416b4e25b51c018ceaa433756ee99b1 (patch) | |
tree | cb6cf2164c8ea900d188c6499c34ff6ab5d75a2c | |
parent | 35a7b0f9b991e884eae81aa7393f9701b7b7b85e (diff) | |
parent | c8e45366b9d0914eb17f759e0bfa7829fd419857 (diff) | |
download | rneovim-8c548c9e5416b4e25b51c018ceaa433756ee99b1.tar.gz rneovim-8c548c9e5416b4e25b51c018ceaa433756ee99b1.tar.bz2 rneovim-8c548c9e5416b4e25b51c018ceaa433756ee99b1.zip |
Merge pull request #18301 from zeertzjq/fix-lua-unmap-crash
fix(mappings): fix double-free when unmapping simplifiable Lua mapping
-rw-r--r-- | src/nvim/getchar.c | 8 | ||||
-rw-r--r-- | test/functional/api/keymap_spec.lua | 42 |
2 files changed, 48 insertions, 2 deletions
diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index 58daa9631a..eb78317ee7 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -3299,7 +3299,9 @@ int buf_do_map(int maptype, MapArguments *args, int mode, bool is_abbrev, buf_T XFREE_CLEAR(mp->m_str); XFREE_CLEAR(mp->m_orig_str); XFREE_CLEAR(mp->m_desc); - NLUA_CLEAR_REF(mp->m_luaref); + if (!mp->m_simplified) { + NLUA_CLEAR_REF(mp->m_luaref); + } mp->m_str = vim_strsave(rhs); mp->m_orig_str = vim_strsave(orig_rhs); @@ -3500,7 +3502,9 @@ static void mapblock_free(mapblock_T **mpp) mp = *mpp; xfree(mp->m_keys); - NLUA_CLEAR_REF(mp->m_luaref); + if (!mp->m_simplified) { + NLUA_CLEAR_REF(mp->m_luaref); + } XFREE_CLEAR(mp->m_str); XFREE_CLEAR(mp->m_orig_str); XFREE_CLEAR(mp->m_desc); diff --git a/test/functional/api/keymap_spec.lua b/test/functional/api/keymap_spec.lua index a11b5306f4..4fb2d55a76 100644 --- a/test/functional/api/keymap_spec.lua +++ b/test/functional/api/keymap_spec.lua @@ -874,6 +874,27 @@ describe('nvim_set_keymap, nvim_del_keymap', function() eq('\nNo mapping found', helpers.exec_capture('nmap asdf')) end) + it('no double-free when unmapping simplifiable lua mappings', function() + eq(0, exec_lua [[ + GlobalCount = 0 + vim.api.nvim_set_keymap('n', '<C-I>', '', {callback = function() GlobalCount = GlobalCount + 1 end }) + return GlobalCount + ]]) + + feed('<C-I>\n') + + eq(1, exec_lua[[return GlobalCount]]) + + exec_lua [[ + vim.api.nvim_del_keymap('n', '<C-I>') + ]] + + feed('<C-I>\n') + + eq(1, exec_lua[[return GlobalCount]]) + eq('\nNo mapping found', helpers.exec_capture('nmap <C-I>')) + end) + it('can set descriptions on keymaps', function() meths.set_keymap('n', 'lhs', 'rhs', {desc="map description"}) eq(generate_mapargs('n', 'lhs', 'rhs', {desc="map description"}), get_mapargs('n', 'lhs')) @@ -1040,4 +1061,25 @@ describe('nvim_buf_set_keymap, nvim_buf_del_keymap', function() eq(1, exec_lua[[return GlobalCount]]) eq('\nNo mapping found', helpers.exec_capture('nmap asdf')) end) + + it('no double-free when unmapping simplifiable lua mappings', function() + eq(0, exec_lua [[ + GlobalCount = 0 + vim.api.nvim_buf_set_keymap(0, 'n', '<C-I>', '', {callback = function() GlobalCount = GlobalCount + 1 end }) + return GlobalCount + ]]) + + feed('<C-I>\n') + + eq(1, exec_lua[[return GlobalCount]]) + + exec_lua [[ + vim.api.nvim_buf_del_keymap(0, 'n', '<C-I>') + ]] + + feed('<C-I>\n') + + eq(1, exec_lua[[return GlobalCount]]) + eq('\nNo mapping found', helpers.exec_capture('nmap <C-I>')) + end) end) |