diff --git a/src/nvim/api/autocmd.c b/src/nvim/api/autocmd.c index 45972ec8ea..0db9a63e7a 100644 --- a/src/nvim/api/autocmd.c +++ b/src/nvim/api/autocmd.c @@ -460,7 +460,7 @@ Integer nvim_create_autocmd(uint64_t channel_id, Object event, Dict(create_autoc cb.data.luaref = api_new_luaref(callback->data.luaref); } else if (callback->type == kObjectTypeString) { cb.type = kCallbackFuncref; - cb.data.funcref = vim_strsave((char_u *)callback->data.string.data); + cb.data.funcref = (char_u *)string_to_cstr(callback->data.string); } else { api_set_error(err, kErrorTypeException, @@ -474,7 +474,7 @@ Integer nvim_create_autocmd(uint64_t channel_id, Object event, Dict(create_autoc Object *command = &opts->command; if (command->type == kObjectTypeString) { aucmd.type = CALLABLE_EX; - aucmd.callable.cmd = vim_strsave((char_u *)command->data.string.data); + aucmd.callable.cmd = (char_u *)string_to_cstr(command->data.string); } else { api_set_error(err, kErrorTypeValidation, @@ -814,7 +814,7 @@ void nvim_exec_autocmds(Object event, Dict(exec_autocmds) *opts, Error *err) goto cleanup; } - pattern = vim_strsave((char_u *)opts->pattern.data.string.data); + pattern = (char_u *)string_to_cstr(opts->pattern.data.string); set_pattern = true; } diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index a07b5b6e3a..a89a254f20 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -488,6 +488,16 @@ String cstr_to_string(const char *str) }; } +/// Copies a String to an allocated, NUL-terminated C string. +/// +/// @param str the String to copy +/// @return the resulting C string +char *string_to_cstr(String str) + FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT +{ + return xstrndup(str.data, str.size); +} + /// Copies buffer to an allocated String. /// The resulting string is also NUL-terminated, to facilitate interoperating /// with code using C strings. @@ -628,7 +638,7 @@ void modify_keymap(uint64_t channel_id, Buffer buffer, bool is_unmap, String mod (char_u *)rhs.data, rhs.size, lua_funcref, CPO_TO_CPO_FLAGS, &parsed_args); if (opts != NULL && opts->desc.type == kObjectTypeString) { - parsed_args.desc = xstrdup(opts->desc.data.string.data); + parsed_args.desc = string_to_cstr(opts->desc.data.string); } else { parsed_args.desc = NULL; } diff --git a/src/nvim/api/ui.c b/src/nvim/api/ui.c index f449ccc3c7..d1b86ed328 100644 --- a/src/nvim/api/ui.c +++ b/src/nvim/api/ui.c @@ -261,7 +261,7 @@ static void ui_set_option(UI *ui, bool init, String name, Object value, Error *e api_set_error(error, kErrorTypeValidation, "term_name must be a String"); return; } - set_tty_option("term", xstrdup(value.data.string.data)); + set_tty_option("term", string_to_cstr(value.data.string)); return; } diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 061653c5af..d9ab097316 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -2522,7 +2522,7 @@ Dictionary nvim_parse_cmd(String str, Dictionary opts, Error *err) // Parse command line exarg_T ea; CmdParseInfo cmdinfo; - char_u *cmdline = vim_strsave((char_u *)str.data); + char_u *cmdline = (char_u *)string_to_cstr(str); if (!parse_cmdline(cmdline, &ea, &cmdinfo)) { api_set_error(err, kErrorTypeException, "Error while parsing command line"); diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 2c4d527fdf..9cdc299ea7 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1647,9 +1647,7 @@ int nlua_expand_pat(expand_T *xp, char_u *pat, int *num_results, char_u ***resul goto cleanup_array; } - GA_APPEND(char_u *, - &result_array, - vim_strsave((char_u *)v.data.string.data)); + GA_APPEND(char_u *, &result_array, (char_u *)string_to_cstr(v.data.string)); } xp->xp_pattern += prefix_len;