patch 9.1.1518: getcompletiontype() may crash

Problem:  getcompletiontype() crashes when no completion is available
          (after v9.1.1509).
Solution: Don't call set_expand_context() (zeertzjq)

fixes: #17681
closes: #17684

Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
zeertzjq
2025-07-06 20:26:56 +02:00
committed by Christian Brabandt
parent d6a5edd613
commit e2c0f81dd0
8 changed files with 53 additions and 50 deletions

View File

@ -1,4 +1,4 @@
*builtin.txt* For Vim version 9.1. Last change: 2025 Jul 05 *builtin.txt* For Vim version 9.1. Last change: 2025 Jul 06
VIM REFERENCE MANUAL by Bram Moolenaar VIM REFERENCE MANUAL by Bram Moolenaar
@ -4212,8 +4212,8 @@ getcmdcompltype() *getcmdcompltype()*
|getcmdprompt()|, |getcmdcomplpat()| and |setcmdline()|. |getcmdprompt()|, |getcmdcomplpat()| and |setcmdline()|.
Returns an empty string when completion is not defined. Returns an empty string when completion is not defined.
To get the type of the command-line completion for the To get the type of the command-line completion for a specified
specified string, use |getcompletiontype()|. string, use |getcompletiontype()|.
Return type: |String| Return type: |String|

View File

@ -4577,10 +4577,7 @@ f_getcompletiontype(typval_T *argvars, typval_T *rettv)
cmdline_len = (int)STRLEN(pat); cmdline_len = (int)STRLEN(pat);
set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE); set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); rettv->vval.v_string = cmdcomplete_type_to_str(xpc.xp_context, xpc.xp_arg);
xpc.xp_col = cmdline_len;
rettv->vval.v_string = get_cmdline_completion(&xpc);
ExpandCleanup(&xpc); ExpandCleanup(&xpc);
} }

View File

@ -4305,37 +4305,30 @@ get_cmdline_completion_pattern(void)
/* /*
* Get the command-line completion type. * Get the command-line completion type.
*/ */
char_u * static char_u *
get_cmdline_completion(expand_T *xpc) get_cmdline_completion(void)
{ {
int xp_context; cmdline_info_T *p;
int xp_context;
xp_context = xpc->xp_context; if (cmdline_star > 0)
return NULL;
p = get_ccline_ptr();
if (p == NULL || p->xpc == NULL)
return NULL;
xp_context = p->xpc->xp_context;
if (xp_context == EXPAND_NOTHING) if (xp_context == EXPAND_NOTHING)
{ {
set_expand_context(xpc); set_expand_context(p->xpc);
xp_context = xpc->xp_context; xp_context = p->xpc->xp_context;
xpc->xp_context = EXPAND_NOTHING; p->xpc->xp_context = EXPAND_NOTHING;
} }
if (xp_context == EXPAND_UNSUCCESSFUL) if (xp_context == EXPAND_UNSUCCESSFUL)
return NULL; return NULL;
char_u *cmd_compl = cmdcomplete_type_to_str(xp_context); return cmdcomplete_type_to_str(xp_context, p->xpc->xp_arg);
if (cmd_compl == NULL)
return NULL;
if (xp_context == EXPAND_USER_LIST || xp_context == EXPAND_USER_DEFINED)
{
char_u *buffer;
buffer = alloc(STRLEN(cmd_compl) + STRLEN(xpc->xp_arg) + 2);
if (buffer == NULL)
return NULL;
sprintf((char *)buffer, "%s,%s", cmd_compl, xpc->xp_arg);
return buffer;
}
return vim_strsave(cmd_compl);
} }
/* /*
@ -4354,16 +4347,8 @@ f_getcmdcomplpat(typval_T *argvars UNUSED, typval_T *rettv)
void void
f_getcmdcompltype(typval_T *argvars UNUSED, typval_T *rettv) f_getcmdcompltype(typval_T *argvars UNUSED, typval_T *rettv)
{ {
cmdline_info_T *p;
rettv->v_type = VAR_STRING; rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL; rettv->vval.v_string = get_cmdline_completion();
p = get_ccline_ptr();
if (cmdline_star > 0 || p == NULL || p->xpc == NULL)
return;
rettv->vval.v_string = get_cmdline_completion(p->xpc);
} }
/* /*

View File

@ -30,7 +30,6 @@ char_u *vim_strsave_fnameescape(char_u *fname, int what);
void escape_fname(char_u **pp); void escape_fname(char_u **pp);
void tilde_replace(char_u *orig_pat, int num_files, char_u **files); void tilde_replace(char_u *orig_pat, int num_files, char_u **files);
cmdline_info_T *get_cmdline_info(void); cmdline_info_T *get_cmdline_info(void);
char_u *get_cmdline_completion(expand_T *xpc);
void f_getcmdcomplpat(typval_T *argvars, typval_T *rettv); void f_getcmdcomplpat(typval_T *argvars, typval_T *rettv);
void f_getcmdcompltype(typval_T *argvars, typval_T *rettv); void f_getcmdcompltype(typval_T *argvars, typval_T *rettv);
void f_getcmdline(typval_T *argvars, typval_T *rettv); void f_getcmdline(typval_T *argvars, typval_T *rettv);

View File

@ -9,7 +9,7 @@ char_u *get_user_cmd_addr_type(expand_T *xp, int idx);
char_u *get_user_cmd_flags(expand_T *xp, int idx); char_u *get_user_cmd_flags(expand_T *xp, int idx);
char_u *get_user_cmd_nargs(expand_T *xp, int idx); char_u *get_user_cmd_nargs(expand_T *xp, int idx);
char_u *get_user_cmd_complete(expand_T *xp, int idx); char_u *get_user_cmd_complete(expand_T *xp, int idx);
char_u *cmdcomplete_type_to_str(int expand); char_u *cmdcomplete_type_to_str(int expand, char_u *arg);
int cmdcomplete_str_to_type(char_u *complete_str); int cmdcomplete_str_to_type(char_u *complete_str);
char *uc_fun_cmd(void); char *uc_fun_cmd(void);
int parse_compl_arg(char_u *value, int vallen, int *complp, long *argt, char_u **compl_arg); int parse_compl_arg(char_u *value, int vallen, int *complp, long *argt, char_u **compl_arg);

View File

@ -880,12 +880,13 @@ endfunc
func Test_getcompletiontype() func Test_getcompletiontype()
call assert_fails('call getcompletiontype()', 'E119:') call assert_fails('call getcompletiontype()', 'E119:')
call assert_fails('call getcompletiontype({})', 'E1174:') call assert_fails('call getcompletiontype({})', 'E1174:')
call assert_equal(getcompletiontype(''), 'command') call assert_equal('command', getcompletiontype(''))
call assert_equal(getcompletiontype('dummy '), '') call assert_equal('', getcompletiontype('dummy '))
call assert_equal(getcompletiontype('cd '), 'dir_in_path') call assert_equal('', getcompletiontype('ls '))
call assert_equal(getcompletiontype('let v:n'), 'var') call assert_equal('dir_in_path', getcompletiontype('cd '))
call assert_equal(getcompletiontype('call tag'), 'function') call assert_equal('var', getcompletiontype('let v:n'))
call assert_equal(getcompletiontype('help '), 'help') call assert_equal('function', getcompletiontype('call tag'))
call assert_equal('help', getcompletiontype('help '))
endfunc endfunc
func Test_multibyte_expression() func Test_multibyte_expression()
@ -4202,6 +4203,8 @@ func Test_custom_completion()
call feedkeys(":Test1 \<C-R>=Check_custom_completion()\<CR>\<Esc>", "xt") call feedkeys(":Test1 \<C-R>=Check_custom_completion()\<CR>\<Esc>", "xt")
call feedkeys(":Test2 \<C-R>=Check_customlist_completion()\<CR>\<Esc>", "xt") call feedkeys(":Test2 \<C-R>=Check_customlist_completion()\<CR>\<Esc>", "xt")
call assert_equal('custom,CustomComplete1', getcompletiontype('Test1 '))
call assert_equal('customlist,CustomComplete2', getcompletiontype('Test2 '))
call assert_fails("call getcompletion('', 'custom')", 'E475:') call assert_fails("call getcompletion('', 'custom')", 'E475:')
call assert_fails("call getcompletion('', 'customlist')", 'E475:') call assert_fails("call getcompletion('', 'customlist')", 'E475:')

View File

@ -486,16 +486,33 @@ get_commandtype(int expand)
#ifdef FEAT_EVAL #ifdef FEAT_EVAL
/* /*
* Get the name of completion type "expand" as a string. * 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 or on allocation failure.
*/ */
char_u * char_u *
cmdcomplete_type_to_str(int expand) cmdcomplete_type_to_str(int expand, char_u *compl_arg)
{ {
keyvalue_T *kv; keyvalue_T *kv;
char_u *cmd_compl;
kv = get_commandtype(expand); kv = get_commandtype(expand);
if (kv == NULL || kv->value.string == NULL)
return NULL;
return (kv == NULL) ? NULL : kv->value.string; cmd_compl = kv->value.string;
if (expand == EXPAND_USER_LIST || expand == EXPAND_USER_DEFINED)
{
char_u *buffer;
buffer = alloc(STRLEN(cmd_compl) + STRLEN(compl_arg) + 2);
if (buffer == NULL)
return NULL;
sprintf((char *)buffer, "%s,%s", cmd_compl, compl_arg);
return buffer;
}
return vim_strsave(cmd_compl);
} }
/* /*

View File

@ -719,6 +719,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
1518,
/**/ /**/
1517, 1517,
/**/ /**/