From 77118d0da8badc4135be430f4cbb15bc95bc760f Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Thu, 20 Apr 2023 21:17:25 +0100 Subject: fix(api): use text_locked() to check textlock Problem: some API functions that check textlock (usually those that can change curwin or curbuf) can break the cmdwin. Solution: make FUNC_API_CHECK_TEXTLOCK call text_locked() instead, which already checks for textlock, cmdwin and `` status. Add FUNC_API_TEXTLOCK_ALLOW_CMDWIN to allow such functions to be usable in the cmdwin if they can work properly there; the opt-in nature of this attribute should hopefully help mitigate future bugs. Also fix a regression in #22634 that made functions checking textlock usable in `` mappings, and rename FUNC_API_CHECK_TEXTLOCK to FUNC_API_TEXTLOCK. --- src/nvim/api/buffer.c | 4 ++-- src/nvim/api/vim.c | 14 +++++++------- src/nvim/api/win_config.c | 2 +- src/nvim/api/window.c | 6 +++--- src/nvim/func_attr.h | 6 ++++-- src/nvim/generators/c_grammar.lua | 3 ++- src/nvim/generators/gen_api_dispatch.lua | 25 ++++++++++++++++++++----- 7 files changed, 39 insertions(+), 21 deletions(-) (limited to 'src/nvim') diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 8774aee20b..7cd9686544 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -347,7 +347,7 @@ end: void nvim_buf_set_lines(uint64_t channel_id, Buffer buffer, Integer start, Integer end, Boolean strict_indexing, ArrayOf(String) replacement, Error *err) FUNC_API_SINCE(1) - FUNC_API_CHECK_TEXTLOCK + FUNC_API_TEXTLOCK_ALLOW_CMDWIN { buf_T *buf = find_buffer_by_handle(buffer, err); @@ -1061,7 +1061,7 @@ Boolean nvim_buf_is_loaded(Buffer buffer) /// - unload: Unloaded only, do not delete. See |:bunload| void nvim_buf_delete(Buffer buffer, Dictionary opts, Error *err) FUNC_API_SINCE(7) - FUNC_API_CHECK_TEXTLOCK + FUNC_API_TEXTLOCK { buf_T *buf = find_buffer_by_handle(buffer, err); diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 8d34a34e8a..eacc833c58 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -612,7 +612,7 @@ String nvim_get_current_line(Error *err) /// @param[out] err Error details, if any void nvim_set_current_line(String line, Error *err) FUNC_API_SINCE(1) - FUNC_API_CHECK_TEXTLOCK + FUNC_API_TEXTLOCK_ALLOW_CMDWIN { buffer_set_line(curbuf->handle, curwin->w_cursor.lnum - 1, line, err); } @@ -622,7 +622,7 @@ void nvim_set_current_line(String line, Error *err) /// @param[out] err Error details, if any void nvim_del_current_line(Error *err) FUNC_API_SINCE(1) - FUNC_API_CHECK_TEXTLOCK + FUNC_API_TEXTLOCK_ALLOW_CMDWIN { buffer_del_line(curbuf->handle, curwin->w_cursor.lnum - 1, err); } @@ -803,7 +803,7 @@ Buffer nvim_get_current_buf(void) /// @param[out] err Error details, if any void nvim_set_current_buf(Buffer buffer, Error *err) FUNC_API_SINCE(1) - FUNC_API_CHECK_TEXTLOCK + FUNC_API_TEXTLOCK { buf_T *buf = find_buffer_by_handle(buffer, err); @@ -858,7 +858,7 @@ Window nvim_get_current_win(void) /// @param[out] err Error details, if any void nvim_set_current_win(Window window, Error *err) FUNC_API_SINCE(1) - FUNC_API_CHECK_TEXTLOCK + FUNC_API_TEXTLOCK { win_T *win = find_window_by_handle(window, err); @@ -1084,7 +1084,7 @@ Tabpage nvim_get_current_tabpage(void) /// @param[out] err Error details, if any void nvim_set_current_tabpage(Tabpage tabpage, Error *err) FUNC_API_SINCE(1) - FUNC_API_CHECK_TEXTLOCK + FUNC_API_TEXTLOCK { tabpage_T *tp = find_tab_by_handle(tabpage, err); @@ -1126,7 +1126,7 @@ void nvim_set_current_tabpage(Tabpage tabpage, Error *err) /// - false: Client must cancel the paste. Boolean nvim_paste(String data, Boolean crlf, Integer phase, Error *err) FUNC_API_SINCE(6) - FUNC_API_CHECK_TEXTLOCK + FUNC_API_TEXTLOCK_ALLOW_CMDWIN { static bool draining = false; bool cancel = false; @@ -1197,7 +1197,7 @@ theend: /// @param[out] err Error details, if any void nvim_put(ArrayOf(String) lines, String type, Boolean after, Boolean follow, Error *err) FUNC_API_SINCE(6) - FUNC_API_CHECK_TEXTLOCK + FUNC_API_TEXTLOCK_ALLOW_CMDWIN { yankreg_T *reg = xcalloc(1, sizeof(yankreg_T)); VALIDATE_S((prepare_yankreg_from_object(reg, type, lines.size)), "type", type.data, { diff --git a/src/nvim/api/win_config.c b/src/nvim/api/win_config.c index 8e4fbb6779..6ca36a0daf 100644 --- a/src/nvim/api/win_config.c +++ b/src/nvim/api/win_config.c @@ -158,7 +158,7 @@ /// @return Window handle, or 0 on error Window nvim_open_win(Buffer buffer, Boolean enter, Dict(float_config) *config, Error *err) FUNC_API_SINCE(6) - FUNC_API_CHECK_TEXTLOCK + FUNC_API_TEXTLOCK { FloatConfig fconfig = FLOAT_CONFIG_INIT; if (!parse_float_config(config, &fconfig, false, true, err)) { diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index e5a824fec9..5fb07ecbc8 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -48,7 +48,7 @@ Buffer nvim_win_get_buf(Window window, Error *err) /// @param[out] err Error details, if any void nvim_win_set_buf(Window window, Buffer buffer, Error *err) FUNC_API_SINCE(5) - FUNC_API_CHECK_TEXTLOCK + FUNC_API_TEXTLOCK { win_set_buf(window, buffer, false, err); } @@ -351,7 +351,7 @@ Boolean nvim_win_is_valid(Window window) /// @param[out] err Error details, if any void nvim_win_hide(Window window, Error *err) FUNC_API_SINCE(7) - FUNC_API_CHECK_TEXTLOCK + FUNC_API_TEXTLOCK { win_T *win = find_window_by_handle(window, err); if (!win) { @@ -383,7 +383,7 @@ void nvim_win_hide(Window window, Error *err) /// @param[out] err Error details, if any void nvim_win_close(Window window, Boolean force, Error *err) FUNC_API_SINCE(6) - FUNC_API_CHECK_TEXTLOCK + FUNC_API_TEXTLOCK_ALLOW_CMDWIN { win_T *win = find_window_by_handle(window, err); if (!win) { diff --git a/src/nvim/func_attr.h b/src/nvim/func_attr.h index 57e5766f67..9ac4ee28d6 100644 --- a/src/nvim/func_attr.h +++ b/src/nvim/func_attr.h @@ -226,8 +226,10 @@ # define FUNC_API_REMOTE_ONLY /// API function not exposed in Vimscript/remote. # define FUNC_API_LUA_ONLY -/// API function checked textlock. -# define FUNC_API_CHECK_TEXTLOCK +/// API function fails during textlock. +# define FUNC_API_TEXTLOCK +/// API function fails during textlock, but allows cmdwin. +# define FUNC_API_TEXTLOCK_ALLOW_CMDWIN /// API function introduced at the given API level. # define FUNC_API_SINCE(X) /// API function deprecated since the given API level. diff --git a/src/nvim/generators/c_grammar.lua b/src/nvim/generators/c_grammar.lua index d83658e977..f33da452ff 100644 --- a/src/nvim/generators/c_grammar.lua +++ b/src/nvim/generators/c_grammar.lua @@ -47,7 +47,8 @@ local c_proto = Ct( (fill * Cg((P('FUNC_API_NOEXPORT') * Cc(true)), 'noexport') ^ -1) * (fill * Cg((P('FUNC_API_REMOTE_ONLY') * Cc(true)), 'remote_only') ^ -1) * (fill * Cg((P('FUNC_API_LUA_ONLY') * Cc(true)), 'lua_only') ^ -1) * - (fill * Cg((P('FUNC_API_CHECK_TEXTLOCK') * Cc(true)), 'check_textlock') ^ -1) * + (fill * (Cg(P('FUNC_API_TEXTLOCK_ALLOW_CMDWIN') * Cc(true), 'textlock_allow_cmdwin') + + Cg(P('FUNC_API_TEXTLOCK') * Cc(true), 'textlock')) ^ -1) * (fill * Cg((P('FUNC_API_REMOTE_IMPL') * Cc(true)), 'remote_impl') ^ -1) * (fill * Cg((P('FUNC_API_COMPOSITOR_IMPL') * Cc(true)), 'compositor_impl') ^ -1) * (fill * Cg((P('FUNC_API_CLIENT_IMPL') * Cc(true)), 'client_impl') ^ -1) * diff --git a/src/nvim/generators/gen_api_dispatch.lua b/src/nvim/generators/gen_api_dispatch.lua index 244921de31..bf211f5273 100644 --- a/src/nvim/generators/gen_api_dispatch.lua +++ b/src/nvim/generators/gen_api_dispatch.lua @@ -210,10 +210,11 @@ local keysets_defs = io.open(keysets_outputf, 'wb') -- so that the dispatcher can find the C functions that you are creating! -- =========================================================================== output:write([[ +#include "nvim/ex_docmd.h" +#include "nvim/ex_getln.h" #include "nvim/log.h" #include "nvim/map.h" #include "nvim/msgpack_rpc/helpers.h" -#include "nvim/vim.h" #include "nvim/api/autocmd.h" #include "nvim/api/buffer.h" @@ -389,8 +390,13 @@ for i = 1, #functions do args[#args + 1] = converted end - if fn.check_textlock then - output:write('\n if (textlock != 0) {') + if fn.textlock then + output:write('\n if (text_locked()) {') + output:write('\n api_set_error(error, kErrorTypeException, "%s", get_text_locked_msg());') + output:write('\n goto cleanup;') + output:write('\n }\n') + elseif fn.textlock_allow_cmdwin then + output:write('\n if (textlock != 0 || expr_map_locked()) {') output:write('\n api_set_error(error, kErrorTypeException, "%s", e_textlock);') output:write('\n goto cleanup;') output:write('\n }\n') @@ -525,6 +531,8 @@ output:write([[ #include #include +#include "nvim/ex_docmd.h" +#include "nvim/ex_getln.h" #include "nvim/func_attr.h" #include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" @@ -565,9 +573,16 @@ local function process_function(fn) ]], fn.name)) end - if fn.check_textlock then + if fn.textlock then + write_shifted_output(output, [[ + if (text_locked()) { + api_set_error(&err, kErrorTypeException, "%s", get_text_locked_msg()); + goto exit_0; + } + ]]) + elseif fn.textlock_allow_cmdwin then write_shifted_output(output, [[ - if (textlock != 0) { + if (textlock != 0 || expr_map_locked()) { api_set_error(&err, kErrorTypeException, "%s", e_textlock); goto exit_0; } -- cgit From aa4e47f704c53ab1d825260d2bf34e2872e3ca89 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Fri, 23 Jun 2023 22:32:07 +0100 Subject: fix(api): disallow some more functions during textlock Problem: nvim_buf_set_text(), nvim_open_term() and termopen() all change buffer text, which is forbidden during textlock. Additionally, nvim_open_term() and termopen() may be used to convert the cmdwin buffer into a terminal buffer, which is weird. Solution: Allow nvim_buf_set_text() and nvim_open_term() in the cmdwin, but disallow nvim_open_term() from converting the cmdwin buffer into a terminal buffer. termopen() is not allowed in the cmdwin (as it always operates on curbuf), so just check text_locked(). Also happens to improve the error in #21055: nvim_buf_set_text() was callable during textlock, but happened to check textlock indirectly via u_save(); however, this caused the error to be overwritten by an unhelpful "Failed to save undo information" message when msg_list == NULL (e.g: an `` mapping invoked outside of do_cmdline()). --- src/nvim/api/buffer.c | 1 + src/nvim/api/vim.c | 6 ++++++ src/nvim/eval/funcs.c | 5 ++++- 3 files changed, 11 insertions(+), 1 deletion(-) (limited to 'src/nvim') diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 7cd9686544..02b97b0ae1 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -515,6 +515,7 @@ end: void nvim_buf_set_text(uint64_t channel_id, Buffer buffer, Integer start_row, Integer start_col, Integer end_row, Integer end_col, ArrayOf(String) replacement, Error *err) FUNC_API_SINCE(7) + FUNC_API_TEXTLOCK_ALLOW_CMDWIN { MAXSIZE_TEMP_ARRAY(scratch, 1); if (replacement.size == 0) { diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index eacc833c58..8c40e5ccd7 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -951,12 +951,18 @@ fail: /// @return Channel id, or 0 on error Integer nvim_open_term(Buffer buffer, DictionaryOf(LuaRef) opts, Error *err) FUNC_API_SINCE(7) + FUNC_API_TEXTLOCK_ALLOW_CMDWIN { buf_T *buf = find_buffer_by_handle(buffer, err); if (!buf) { return 0; } + if (cmdwin_type != 0 && buf == curbuf) { + api_set_error(err, kErrorTypeException, "%s", _(e_cmdwin)); + return 0; + } + LuaRef cb = LUA_NOREF; for (size_t i = 0; i < opts.size; i++) { String k = opts.items[i].key; diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index f348a61075..7c3c5cc274 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -8385,7 +8385,10 @@ static void f_termopen(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) if (check_secure()) { return; } - + if (text_locked()) { + text_locked_msg(); + return; + } if (curbuf->b_changed) { emsg(_("Can only call this function in an unmodified buffer")); return; -- cgit