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:
zeertzjq
2025-07-07 06:46:05 +08:00
committed by GitHub
parent 2e2ac49c57
commit 6fd2a3040f
7 changed files with 53 additions and 43 deletions

View File

@@ -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) {