From fe9cbcb3a5c82932ecfb8f49d07e98a1fc2b31e5 Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Sat, 25 Mar 2023 18:58:48 +0200 Subject: feat(api): nvim_exec2(), deprecate nvim_exec() #19032 Problem: The signature of nvim_exec() is not extensible per ":help api-contract". Solution: Introduce nvim_exec2() and deprecate nvim_exec(). --- src/nvim/context.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/nvim/context.c') diff --git a/src/nvim/context.c b/src/nvim/context.c index 9de6c16536..b13a331eff 100644 --- a/src/nvim/context.c +++ b/src/nvim/context.c @@ -10,6 +10,7 @@ #include #include "nvim/api/private/converter.h" +#include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" #include "nvim/api/vimscript.h" #include "nvim/context.h" @@ -271,8 +272,10 @@ static inline void ctx_save_funcs(Context *ctx, bool scriptonly) size_t cmd_len = sizeof("func! ") + strlen(name); char *cmd = xmalloc(cmd_len); snprintf(cmd, cmd_len, "func! %s", name); - String func_body = nvim_exec(VIML_INTERNAL_CALL, cstr_as_string(cmd), - true, &err); + Dict(exec_opts) opts = { 0 }; + opts.output = BOOLEAN_OBJ(true); + String func_body = exec_impl(VIML_INTERNAL_CALL, cstr_as_string(cmd), + &opts, &err); xfree(cmd); if (!ERROR_SET(&err)) { ADD(ctx->funcs, STRING_OBJ(func_body)); -- cgit From b3d5138fd0066fda26ef7724a542ae45eb42fc84 Mon Sep 17 00:00:00 2001 From: Famiu Haque Date: Wed, 7 Jun 2023 06:05:16 +0600 Subject: refactor(options): remove `getoption_T` and introduce `OptVal` (#23850) Removes the `getoption_T` struct and also introduces the `OptVal` struct to unify the methods of getting/setting different option value types. This is the first of many PRs to reduce code duplication in the Vim option code as well as to make options easier to maintain. It also increases the flexibility and extensibility of options. Which opens the door for things like Array and Dictionary options. --- src/nvim/context.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/nvim/context.c') diff --git a/src/nvim/context.c b/src/nvim/context.c index b13a331eff..f7b3491be7 100644 --- a/src/nvim/context.c +++ b/src/nvim/context.c @@ -143,9 +143,8 @@ bool ctx_restore(Context *ctx, const int flags) free_ctx = true; } - char *op_shada; - get_option_value("shada", NULL, &op_shada, NULL, OPT_GLOBAL); - set_option_value("shada", 0L, "!,'100,%", OPT_GLOBAL); + OptVal op_shada = get_option_value("shada", NULL, OPT_GLOBAL, NULL); + set_option_value("shada", STATIC_CSTR_AS_OPTVAL("!,'100,%"), OPT_GLOBAL); if (flags & kCtxRegs) { ctx_restore_regs(ctx); @@ -171,8 +170,8 @@ bool ctx_restore(Context *ctx, const int flags) ctx_free(ctx); } - set_option_value("shada", 0L, op_shada, OPT_GLOBAL); - xfree(op_shada); + set_option_value("shada", op_shada, OPT_GLOBAL); + optval_free(op_shada); return true; } -- cgit From 7bc93e0e2f246dd78026a3472d929a0fe450f70d Mon Sep 17 00:00:00 2001 From: bfredl Date: Tue, 1 Aug 2023 14:01:19 +0200 Subject: refactor(api): use typed keysets Initially this is just for geting rid of boilerplate, but eventually the types could get exposed as metadata --- src/nvim/context.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/nvim/context.c') diff --git a/src/nvim/context.c b/src/nvim/context.c index f7b3491be7..ca28ed9970 100644 --- a/src/nvim/context.c +++ b/src/nvim/context.c @@ -271,8 +271,7 @@ static inline void ctx_save_funcs(Context *ctx, bool scriptonly) size_t cmd_len = sizeof("func! ") + strlen(name); char *cmd = xmalloc(cmd_len); snprintf(cmd, cmd_len, "func! %s", name); - Dict(exec_opts) opts = { 0 }; - opts.output = BOOLEAN_OBJ(true); + Dict(exec_opts) opts = { .output = true }; String func_body = exec_impl(VIML_INTERNAL_CALL, cstr_as_string(cmd), &opts, &err); xfree(cmd); -- cgit From cf8b2c0e74fd5e723b0c15c2ce84e6900fd322d3 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 30 Sep 2023 12:05:28 +0800 Subject: build(iwyu): add a few more _defs.h mappings (#25435) --- src/nvim/context.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/context.c') diff --git a/src/nvim/context.c b/src/nvim/context.c index ca28ed9970..fe5a49faa2 100644 --- a/src/nvim/context.c +++ b/src/nvim/context.c @@ -9,6 +9,7 @@ #include #include +#include "nvim/api/keysets.h" #include "nvim/api/private/converter.h" #include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" @@ -16,7 +17,6 @@ #include "nvim/context.h" #include "nvim/eval/encode.h" #include "nvim/eval/typval.h" -#include "nvim/eval/typval_defs.h" #include "nvim/eval/userfunc.h" #include "nvim/ex_docmd.h" #include "nvim/gettext.h" -- cgit From cd63a9addd6e1114c3524fa041ece560550cfe7b Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 10 Nov 2023 08:39:21 +0800 Subject: refactor: change some xstrndup() and xstrnsave() to xmemdupz() (#25959) When the given length is exactly the number of bytes to copy, xmemdupz() makes the intention clearer. --- src/nvim/context.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/context.c') diff --git a/src/nvim/context.c b/src/nvim/context.c index fe5a49faa2..9b1eee56c5 100644 --- a/src/nvim/context.c +++ b/src/nvim/context.c @@ -330,7 +330,7 @@ static inline msgpack_sbuffer array_to_sbuf(Array array) typval_T list_tv; Error err = ERROR_INIT; - object_to_vim(ARRAY_OBJ(array), &list_tv, &err); + (void)object_to_vim(ARRAY_OBJ(array), &list_tv, &err); if (!encode_vim_list_to_buf(list_tv.vval.v_list, &sbuf.size, &sbuf.data)) { emsg(_("E474: Failed to convert list to msgpack string buffer")); -- cgit From e9b9a86cd5a555943b87f0ba40c4527561c3c124 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 11 Nov 2023 10:21:14 +0800 Subject: fix(context): don't crash on invalid arg to nvim_get_context (#25977) Note: The crash happens in the second test case when using uninitialized memory, and therefore doesn't happen with ASAN. --- src/nvim/context.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'src/nvim/context.c') diff --git a/src/nvim/context.c b/src/nvim/context.c index 9b1eee56c5..f7ed800aa7 100644 --- a/src/nvim/context.c +++ b/src/nvim/context.c @@ -321,24 +321,28 @@ static inline Array sbuf_to_array(msgpack_sbuffer sbuf) /// Convert readfile()-style array to msgpack_sbuffer. /// /// @param[in] array readfile()-style array to convert. +/// @param[out] err Error object. /// /// @return msgpack_sbuffer with conversion result. -static inline msgpack_sbuffer array_to_sbuf(Array array) +static inline msgpack_sbuffer array_to_sbuf(Array array, Error *err) + FUNC_ATTR_NONNULL_ALL { msgpack_sbuffer sbuf; msgpack_sbuffer_init(&sbuf); typval_T list_tv; - Error err = ERROR_INIT; - (void)object_to_vim(ARRAY_OBJ(array), &list_tv, &err); + if (!object_to_vim(ARRAY_OBJ(array), &list_tv, err)) { + return sbuf; + } + assert(list_tv.v_type == VAR_LIST); if (!encode_vim_list_to_buf(list_tv.vval.v_list, &sbuf.size, &sbuf.data)) { - emsg(_("E474: Failed to convert list to msgpack string buffer")); + api_set_error(err, kErrorTypeException, "%s", + "E474: Failed to convert list to msgpack string buffer"); } sbuf.alloc = sbuf.size; tv_clear(&list_tv); - api_clear_error(&err); return sbuf; } @@ -367,9 +371,10 @@ Dictionary ctx_to_dict(Context *ctx) /// /// @param[in] dict Context Dictionary representation. /// @param[out] ctx Context object to store conversion result into. +/// @param[out] err Error object. /// /// @return types of included context items. -int ctx_from_dict(Dictionary dict, Context *ctx) +int ctx_from_dict(Dictionary dict, Context *ctx, Error *err) FUNC_ATTR_NONNULL_ALL { assert(ctx != NULL); @@ -382,16 +387,16 @@ int ctx_from_dict(Dictionary dict, Context *ctx) } if (strequal(item.key.data, "regs")) { types |= kCtxRegs; - ctx->regs = array_to_sbuf(item.value.data.array); + ctx->regs = array_to_sbuf(item.value.data.array, err); } else if (strequal(item.key.data, "jumps")) { types |= kCtxJumps; - ctx->jumps = array_to_sbuf(item.value.data.array); + ctx->jumps = array_to_sbuf(item.value.data.array, err); } else if (strequal(item.key.data, "bufs")) { types |= kCtxBufs; - ctx->bufs = array_to_sbuf(item.value.data.array); + ctx->bufs = array_to_sbuf(item.value.data.array, err); } else if (strequal(item.key.data, "gvars")) { types |= kCtxGVars; - ctx->gvars = array_to_sbuf(item.value.data.array); + ctx->gvars = array_to_sbuf(item.value.data.array, err); } else if (strequal(item.key.data, "funcs")) { types |= kCtxFuncs; ctx->funcs = copy_object(item.value, NULL).data.array; -- cgit From fdaf6bc5573f578d100321dfc084d513884fd73a Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 11 Nov 2023 13:10:26 +0800 Subject: fix(context): don't leak memory on multiple invalid objects (#25979) --- src/nvim/context.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/context.c') diff --git a/src/nvim/context.c b/src/nvim/context.c index f7ed800aa7..acf793c39a 100644 --- a/src/nvim/context.c +++ b/src/nvim/context.c @@ -380,7 +380,7 @@ int ctx_from_dict(Dictionary dict, Context *ctx, Error *err) assert(ctx != NULL); int types = 0; - for (size_t i = 0; i < dict.size; i++) { + for (size_t i = 0; i < dict.size && !ERROR_SET(err); i++) { KeyValuePair item = dict.items[i]; if (item.value.type != kObjectTypeArray) { continue; -- cgit From 353a4be7e84fdc101318215bdcc8a7e780d737fe Mon Sep 17 00:00:00 2001 From: dundargoc Date: Sun, 12 Nov 2023 13:13:58 +0100 Subject: build: remove PVS We already have an extensive suite of static analysis tools we use, which causes a fair bit of redundancy as we get duplicate warnings. PVS is also prone to give false warnings which creates a lot of work to identify and disable. --- src/nvim/context.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/nvim/context.c') diff --git a/src/nvim/context.c b/src/nvim/context.c index acf793c39a..3114fc8ab5 100644 --- a/src/nvim/context.c +++ b/src/nvim/context.c @@ -1,6 +1,3 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - // Context: snapshot of the entire editor state as one big object/map #include -- cgit From bb4b4576e384c71890b4df4fa4f1ae76fad3a59d Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 16 Nov 2023 10:55:54 +0800 Subject: refactor: iwyu (#26062) --- src/nvim/context.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/nvim/context.c') diff --git a/src/nvim/context.c b/src/nvim/context.c index 3114fc8ab5..857f26af37 100644 --- a/src/nvim/context.c +++ b/src/nvim/context.c @@ -16,11 +16,9 @@ #include "nvim/eval/typval.h" #include "nvim/eval/userfunc.h" #include "nvim/ex_docmd.h" -#include "nvim/gettext.h" #include "nvim/hashtab.h" #include "nvim/keycodes.h" #include "nvim/memory.h" -#include "nvim/message.h" #include "nvim/option.h" #include "nvim/shada.h" -- cgit From 8b428ca8b79ebb7b36c3e403ff3bcb6924a635a6 Mon Sep 17 00:00:00 2001 From: dundargoc Date: Mon, 27 Nov 2023 16:00:21 +0100 Subject: build(IWYU): fix includes for func_attr.h --- src/nvim/context.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/nvim/context.c') diff --git a/src/nvim/context.c b/src/nvim/context.c index 857f26af37..6d08c38f61 100644 --- a/src/nvim/context.c +++ b/src/nvim/context.c @@ -16,6 +16,7 @@ #include "nvim/eval/typval.h" #include "nvim/eval/userfunc.h" #include "nvim/ex_docmd.h" +#include "nvim/func_attr.h" #include "nvim/hashtab.h" #include "nvim/keycodes.h" #include "nvim/memory.h" -- cgit From e3f735ef101d670555f44226614a5c3557053b1f Mon Sep 17 00:00:00 2001 From: dundargoc Date: Mon, 27 Nov 2023 20:13:32 +0100 Subject: refactor: fix includes for api/autocmd.h --- src/nvim/context.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/context.c') diff --git a/src/nvim/context.c b/src/nvim/context.c index 6d08c38f61..59309fcf16 100644 --- a/src/nvim/context.c +++ b/src/nvim/context.c @@ -6,7 +6,7 @@ #include #include -#include "nvim/api/keysets.h" +#include "nvim/api/keysets_defs.h" #include "nvim/api/private/converter.h" #include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" -- cgit