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>
diff --git a/src/usercmd.c b/src/usercmd.c
index 9f1efb4..0ed9598 100644
--- a/src/usercmd.c
+++ b/src/usercmd.c
@@ -486,16 +486,33 @@
#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 *
-cmdcomplete_type_to_str(int expand)
+cmdcomplete_type_to_str(int expand, char_u *compl_arg)
{
keyvalue_T *kv;
+ char_u *cmd_compl;
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);
}
/*