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/runtime/doc/builtin.txt b/runtime/doc/builtin.txt
index f38948c..60dbb47 100644
--- a/runtime/doc/builtin.txt
+++ b/runtime/doc/builtin.txt
@@ -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
@@ -4212,8 +4212,8 @@
 		|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()|.
 
 		Return type: |String|
 
diff --git a/src/cmdexpand.c b/src/cmdexpand.c
index 5fa631c..b7597ea 100644
--- a/src/cmdexpand.c
+++ b/src/cmdexpand.c
@@ -4577,10 +4577,7 @@
 
     cmdline_len = (int)STRLEN(pat);
     set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
-    xpc.xp_pattern_len = (int)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);
 }
diff --git a/src/ex_getln.c b/src/ex_getln.c
index f2a51ea..f63ac60 100644
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -4305,37 +4305,30 @@
 /*
  * Get the command-line completion type.
  */
-    char_u *
-get_cmdline_completion(expand_T *xpc)
+    static char_u *
+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)
     {
-	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_u *cmd_compl = cmdcomplete_type_to_str(xp_context);
-    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);
+    return cmdcomplete_type_to_str(xp_context, p->xpc->xp_arg);
 }
 
 /*
@@ -4354,16 +4347,8 @@
     void
 f_getcmdcompltype(typval_T *argvars UNUSED, typval_T *rettv)
 {
-    cmdline_info_T *p;
-
     rettv->v_type = VAR_STRING;
-    rettv->vval.v_string = NULL;
-
-    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();
 }
 
 /*
diff --git a/src/proto/ex_getln.pro b/src/proto/ex_getln.pro
index 9087c83..6c93ac7 100644
--- a/src/proto/ex_getln.pro
+++ b/src/proto/ex_getln.pro
@@ -30,7 +30,6 @@
 void escape_fname(char_u **pp);
 void tilde_replace(char_u *orig_pat, int num_files, char_u **files);
 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_getcmdcompltype(typval_T *argvars, typval_T *rettv);
 void f_getcmdline(typval_T *argvars, typval_T *rettv);
diff --git a/src/proto/usercmd.pro b/src/proto/usercmd.pro
index d0cd2a3..dcf2a1d 100644
--- a/src/proto/usercmd.pro
+++ b/src/proto/usercmd.pro
@@ -9,7 +9,7 @@
 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_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);
 char *uc_fun_cmd(void);
 int parse_compl_arg(char_u *value, int vallen, int *complp, long *argt, char_u **compl_arg);
diff --git a/src/testdir/test_cmdline.vim b/src/testdir/test_cmdline.vim
index cb0bcf7..b3d2936 100644
--- a/src/testdir/test_cmdline.vim
+++ b/src/testdir/test_cmdline.vim
@@ -880,12 +880,13 @@
 func Test_getcompletiontype()
   call assert_fails('call getcompletiontype()', 'E119:')
   call assert_fails('call getcompletiontype({})', 'E1174:')
-  call assert_equal(getcompletiontype(''), 'command')
-  call assert_equal(getcompletiontype('dummy '), '')
-  call assert_equal(getcompletiontype('cd '), 'dir_in_path')
-  call assert_equal(getcompletiontype('let v:n'), 'var')
-  call assert_equal(getcompletiontype('call tag'), 'function')
-  call assert_equal(getcompletiontype('help '), 'help')
+  call assert_equal('command', getcompletiontype(''))
+  call assert_equal('', getcompletiontype('dummy '))
+  call assert_equal('', getcompletiontype('ls '))
+  call assert_equal('dir_in_path', getcompletiontype('cd '))
+  call assert_equal('var', getcompletiontype('let v:n'))
+  call assert_equal('function', getcompletiontype('call tag'))
+  call assert_equal('help', getcompletiontype('help '))
 endfunc
 
 func Test_multibyte_expression()
@@ -4202,6 +4203,8 @@
 
   call feedkeys(":Test1 \<C-R>=Check_custom_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('', 'customlist')", 'E475:')
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);
 }
 
 /*
diff --git a/src/version.c b/src/version.c
index 6bd3f67..ff3b32f 100644
--- a/src/version.c
+++ b/src/version.c
@@ -720,6 +720,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1518,
+/**/
     1517,
 /**/
     1516,