From fd960a33e4d370917ede6b460efb069e663dbbc9 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Sun, 3 Jan 2021 19:14:18 +0100 Subject: [PATCH] fix: check for valid buffer handles in modify_keymap (#13543) Fixes #13541 Neovim would crash when trying to map a key on non existant buffer --- src/nvim/api/private/helpers.c | 4 ++++ test/functional/api/keymap_spec.lua | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index 2c99d3426c..8f224e8c78 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -820,6 +820,10 @@ void modify_keymap(Buffer buffer, bool is_unmap, String mode, String lhs, } buf_T *target_buf = find_buffer_by_handle(buffer, err); + if (!target_buf) { + return; + } + MapArguments parsed_args; memset(&parsed_args, 0, sizeof(parsed_args)); if (parse_keymap_opts(opts, &parsed_args, err)) { diff --git a/test/functional/api/keymap_spec.lua b/test/functional/api/keymap_spec.lua index d8a9c3b411..4194945645 100644 --- a/test/functional/api/keymap_spec.lua +++ b/test/functional/api/keymap_spec.lua @@ -809,4 +809,9 @@ describe('nvim_buf_set_keymap, nvim_buf_del_keymap', function() command('normal lhs') eq({'rhs'}, bufmeths.get_lines(0, 0, 1, 1)) end) + + it("does not crash when setting keymap in a non-existing buffer #13541", function() + pcall_err(bufmeths.set_keymap, 100, '', 'lsh', 'irhs', {}) + helpers.assert_alive() + end) end)