mirror of
https://github.com/neovim/neovim
synced 2025-07-15 16:51:49 +00:00
vim-patch:9.1.1518: getcompletiontype() may crash (#34819)
Problem: getcompletiontype() crashes when no completion is available
(after v9.1.1509).
Solution: Don't call set_expand_context() (zeertzjq)
fixes: vim/vim#17681
closes: vim/vim#17684
e2c0f81dd0
This commit is contained in:
@ -3854,10 +3854,7 @@ void f_getcompletiontype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
||||
|
||||
int cmdline_len = (int)strlen(pat);
|
||||
set_cmd_context(&xpc, (char *)pat, cmdline_len, cmdline_len, false);
|
||||
xpc.xp_pattern_len = strlen(xpc.xp_pattern);
|
||||
xpc.xp_col = cmdline_len;
|
||||
|
||||
rettv->vval.v_string = get_cmdline_completion(&xpc);
|
||||
rettv->vval.v_string = cmdcomplete_type_to_str(xpc.xp_context, xpc.xp_arg);
|
||||
|
||||
ExpandCleanup(&xpc);
|
||||
}
|
||||
|
@ -3760,8 +3760,8 @@ M.funcs = {
|
||||
|getcmdprompt()|, |getcmdcomplpat()| and |setcmdline()|.
|
||||
Returns an empty string when completion is not defined.
|
||||
|
||||
To get the type of the command-line completion for the
|
||||
specified string, use |getcompletiontype()|.
|
||||
To get the type of the command-line completion for a specified
|
||||
string, use |getcompletiontype()|.
|
||||
]=],
|
||||
name = 'getcmdcompltype',
|
||||
params = {},
|
||||
|
@ -4198,31 +4198,28 @@ static char *get_cmdline_completion_pattern(void)
|
||||
}
|
||||
|
||||
/// Get the command-line completion type.
|
||||
char *get_cmdline_completion(expand_T *xpc)
|
||||
static char *get_cmdline_completion(void)
|
||||
{
|
||||
int xp_context = xpc->xp_context;
|
||||
if (cmdline_star > 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
CmdlineInfo *p = get_ccline_ptr();
|
||||
if (p == NULL || p->xpc == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int xp_context = p->xpc->xp_context;
|
||||
if (xp_context == EXPAND_NOTHING) {
|
||||
set_expand_context(xpc);
|
||||
xp_context = xpc->xp_context;
|
||||
xpc->xp_context = EXPAND_NOTHING;
|
||||
set_expand_context(p->xpc);
|
||||
xp_context = p->xpc->xp_context;
|
||||
p->xpc->xp_context = EXPAND_NOTHING;
|
||||
}
|
||||
if (xp_context == EXPAND_UNSUCCESSFUL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *cmd_compl = get_user_cmd_complete(NULL, xp_context);
|
||||
if (cmd_compl == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (xp_context == EXPAND_USER_LIST || xp_context == EXPAND_USER_DEFINED) {
|
||||
size_t buflen = strlen(cmd_compl) + strlen(xpc->xp_arg) + 2;
|
||||
char *buffer = xmalloc(buflen);
|
||||
snprintf(buffer, buflen, "%s,%s", cmd_compl, xpc->xp_arg);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
return xstrdup(cmd_compl);
|
||||
return cmdcomplete_type_to_str(xp_context, p->xpc->xp_arg);
|
||||
}
|
||||
|
||||
/// "getcmdcomplpat()" function
|
||||
@ -4236,14 +4233,7 @@ void f_getcmdcomplpat(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
||||
void f_getcmdcompltype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
||||
{
|
||||
rettv->v_type = VAR_STRING;
|
||||
rettv->vval.v_string = NULL;
|
||||
|
||||
CmdlineInfo *p = get_ccline_ptr();
|
||||
if (cmdline_star > 0 || p == NULL || p->xpc == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
rettv->vval.v_string = get_cmdline_completion(p->xpc);
|
||||
rettv->vval.v_string = get_cmdline_completion();
|
||||
}
|
||||
|
||||
/// "getcmdline()" function
|
||||
|
@ -403,7 +403,7 @@ char *get_user_cmd_nargs(expand_T *xp, int idx)
|
||||
|
||||
static char *get_command_complete(int arg)
|
||||
{
|
||||
if (arg >= (int)(ARRAY_SIZE(command_complete))) {
|
||||
if (arg < 0 || arg >= (int)(ARRAY_SIZE(command_complete))) {
|
||||
return NULL;
|
||||
}
|
||||
return (char *)command_complete[arg];
|
||||
@ -422,6 +422,26 @@ char *get_user_cmd_complete(expand_T *xp, int idx)
|
||||
return cmd_compl;
|
||||
}
|
||||
|
||||
/// Get the name of completion type "expand" as an allocated string.
|
||||
/// "compl_arg" is the function name for "custom" and "customlist" types.
|
||||
/// Returns NULL if no completion is available.
|
||||
char *cmdcomplete_type_to_str(int expand, const char *compl_arg)
|
||||
{
|
||||
char *cmd_compl = get_command_complete(expand);
|
||||
if (cmd_compl == NULL || expand == EXPAND_USER_LUA) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (expand == EXPAND_USER_LIST || expand == EXPAND_USER_DEFINED) {
|
||||
size_t buflen = strlen(cmd_compl) + strlen(compl_arg) + 2;
|
||||
char *buffer = xmalloc(buflen);
|
||||
snprintf(buffer, buflen, "%s,%s", cmd_compl, compl_arg);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
return xstrdup(cmd_compl);
|
||||
}
|
||||
|
||||
int cmdcomplete_str_to_type(const char *complete_str)
|
||||
{
|
||||
if (strncmp(complete_str, "custom,", 7) == 0) {
|
||||
|
Reference in New Issue
Block a user