patch 9.1.1518: getcompletiontype() may crash

Commit: 
https://github.com/vim/vim/commit/e2c0f81dd014b03a40af6b2c42463b7a0d92f5d6
Author: zeertzjq <zeert...@outlook.com>
Date:   Sun Jul 6 20:26:56 2025 +0200

    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 <zeert...@outlook.com>
    Signed-off-by: Christian Brabandt <c...@256bit.org>

diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt
index f38948c75..60dbb4799 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 @@ getcmdcompltype()                                 
*getcmdcompltype()*
                |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 5fa631c22..b7597ea31 100644
--- a/src/cmdexpand.c
+++ b/src/cmdexpand.c
@@ -4577,10 +4577,7 @@ f_getcompletiontype(typval_T *argvars, typval_T *rettv)
 
     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 f2a51ea2c..f63ac6022 100644
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -4305,37 +4305,30 @@ get_cmdline_completion_pattern(void)
 /*
  * 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 (xp_context == EXPAND_NOTHING)
-    {
-       set_expand_context(xpc);
-       xp_context = xpc->xp_context;
-       xpc->xp_context = EXPAND_NOTHING;
-    }
-    if (xp_context == EXPAND_UNSUCCESSFUL)
+    if (cmdline_star > 0)
        return NULL;
 
-    char_u *cmd_compl = cmdcomplete_type_to_str(xp_context);
-    if (cmd_compl == NULL)
+    p = get_ccline_ptr();
+    if (p == NULL || p->xpc == NULL)
        return NULL;
 
-    if (xp_context == EXPAND_USER_LIST || xp_context == EXPAND_USER_DEFINED)
+    xp_context = p->xpc->xp_context;
+    if (xp_context == EXPAND_NOTHING)
     {
-       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;
+       set_expand_context(p->xpc);
+       xp_context = p->xpc->xp_context;
+       p->xpc->xp_context = EXPAND_NOTHING;
     }
+    if (xp_context == EXPAND_UNSUCCESSFUL)
+       return NULL;
 
-    return vim_strsave(cmd_compl);
+    return cmdcomplete_type_to_str(xp_context, p->xpc->xp_arg);
 }
 
 /*
@@ -4354,16 +4347,8 @@ f_getcmdcomplpat(typval_T *argvars UNUSED, typval_T 
*rettv)
     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 9087c837d..6c93ac755 100644
--- a/src/proto/ex_getln.pro
+++ b/src/proto/ex_getln.pro
@@ -30,7 +30,6 @@ char_u *vim_strsave_fnameescape(char_u *fname, int what);
 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 d0cd2a3ac..dcf2a1dbe 100644
--- a/src/proto/usercmd.pro
+++ b/src/proto/usercmd.pro
@@ -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_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 cb0bcf720..b3d293639 100644
--- a/src/testdir/test_cmdline.vim
+++ b/src/testdir/test_cmdline.vim
@@ -880,12 +880,13 @@ endfunc
 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 @@ func Test_custom_completion()
 
   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 9f1efb4b7..0ed9598d8 100644
--- a/src/usercmd.c
+++ b/src/usercmd.c
@@ -486,16 +486,33 @@ get_commandtype(int expand)
 
 #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;
+
+    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 (kv == NULL) ? NULL : kv->value.string;
+    return vim_strsave(cmd_compl);
 }
 
 /*
diff --git a/src/version.c b/src/version.c
index 6bd3f67ee..ff3b32f39 100644
--- a/src/version.c
+++ b/src/version.c
@@ -719,6 +719,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1518,
 /**/
     1517,
 /**/

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_dev+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/vim_dev/E1uYU7o-007Aox-MP%40256bit.org.

Raspunde prin e-mail lui