1e16b4b
To: vim-dev@vim.org
1e16b4b
Subject: Patch 7.2.016
1e16b4b
Fcc: outbox
1e16b4b
From: Bram Moolenaar <Bram@moolenaar.net>
1e16b4b
Mime-Version: 1.0
1e16b4b
Content-Type: text/plain; charset=ISO-8859-1
1e16b4b
Content-Transfer-Encoding: 8bit
1e16b4b
------------
1e16b4b
1e16b4b
Patch 7.2.016
1e16b4b
Problem:    The pattern being completed may be in freed memory when the
1e16b4b
	    command line is being reallocated. (Dominique Pelle)
1e16b4b
Solution:   Keep a pointer to the expand_T in the command line structure.
1e16b4b
	    Don't use <S-Tab> as CTRL-P when there are no results.  Clear the
1e16b4b
	    completion when using a command line from the history.
1e16b4b
Files:	    src/ex_getln.c
1e16b4b
1e16b4b
1e16b4b
*** ../vim-7.2.015/src/ex_getln.c	Fri Aug  8 12:58:59 2008
1e16b4b
--- src/ex_getln.c	Wed Sep 10 22:43:41 2008
1e16b4b
***************
1e16b4b
*** 31,36 ****
1e16b4b
--- 31,38 ----
1e16b4b
      int		cmdattr;	/* attributes for prompt */
1e16b4b
      int		overstrike;	/* Typing mode on the command line.  Shared by
1e16b4b
  				   getcmdline() and put_on_cmdline(). */
1e16b4b
+     expand_T	*xpc;		/* struct being used for expansion, xp_pattern
1e16b4b
+ 				   may point into cmdbuff */
1e16b4b
      int		xp_context;	/* type of expansion */
1e16b4b
  # ifdef FEAT_EVAL
1e16b4b
      char_u	*xp_arg;	/* user-defined expansion arg */
1e16b4b
***************
1e16b4b
*** 38,44 ****
1e16b4b
  # endif
1e16b4b
  };
1e16b4b
  
1e16b4b
! static struct cmdline_info ccline;	/* current cmdline_info */
1e16b4b
  
1e16b4b
  static int	cmd_showtail;		/* Only show path tail in lists ? */
1e16b4b
  
1e16b4b
--- 40,50 ----
1e16b4b
  # endif
1e16b4b
  };
1e16b4b
  
1e16b4b
! /* The current cmdline_info.  It is initialized in getcmdline() and after that
1e16b4b
!  * used by other functions.  When invoking getcmdline() recursively it needs
1e16b4b
!  * to be saved with save_cmdline() and restored with restore_cmdline().
1e16b4b
!  * TODO: make it local to getcmdline() and pass it around. */
1e16b4b
! static struct cmdline_info ccline;
1e16b4b
  
1e16b4b
  static int	cmd_showtail;		/* Only show path tail in lists ? */
1e16b4b
  
1e16b4b
***************
1e16b4b
*** 238,243 ****
1e16b4b
--- 244,250 ----
1e16b4b
      }
1e16b4b
  
1e16b4b
      ExpandInit(&xpc);
1e16b4b
+     ccline.xpc = &xp;;
1e16b4b
  
1e16b4b
  #ifdef FEAT_RIGHTLEFT
1e16b4b
      if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
1e16b4b
***************
1e16b4b
*** 408,416 ****
1e16b4b
  #endif
1e16b4b
  
1e16b4b
  	/*
1e16b4b
! 	 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
1e16b4b
  	 */
1e16b4b
! 	if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles != -1)
1e16b4b
  	    c = Ctrl_P;
1e16b4b
  
1e16b4b
  #ifdef FEAT_WILDMENU
1e16b4b
--- 415,424 ----
1e16b4b
  #endif
1e16b4b
  
1e16b4b
  	/*
1e16b4b
! 	 * When there are matching completions to select <S-Tab> works like
1e16b4b
! 	 * CTRL-P (unless 'wc' is <S-Tab>).
1e16b4b
  	 */
1e16b4b
! 	if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
1e16b4b
  	    c = Ctrl_P;
1e16b4b
  
1e16b4b
  #ifdef FEAT_WILDMENU
1e16b4b
***************
1e16b4b
*** 1513,1518 ****
1e16b4b
--- 1521,1527 ----
1e16b4b
  		    int		old_firstc;
1e16b4b
  
1e16b4b
  		    vim_free(ccline.cmdbuff);
1e16b4b
+ 		    xpc.xp_context = EXPAND_NOTHING;
1e16b4b
  		    if (hiscnt == hislen)
1e16b4b
  			p = lookfor;	/* back to the old one */
1e16b4b
  		    else
1e16b4b
***************
1e16b4b
*** 1839,1844 ****
1e16b4b
--- 1848,1854 ----
1e16b4b
  #endif
1e16b4b
  
1e16b4b
      ExpandCleanup(&xpc);
1e16b4b
+     ccline.xpc = NULL;
1e16b4b
  
1e16b4b
  #ifdef FEAT_SEARCH_EXTRA
1e16b4b
      if (did_incsearch)
1e16b4b
***************
1e16b4b
*** 2508,2513 ****
1e16b4b
--- 2518,2537 ----
1e16b4b
      }
1e16b4b
      mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
1e16b4b
      vim_free(p);
1e16b4b
+ 
1e16b4b
+     if (ccline.xpc != NULL
1e16b4b
+ 	    && ccline.xpc->xp_pattern != NULL
1e16b4b
+ 	    && ccline.xpc->xp_context != EXPAND_NOTHING
1e16b4b
+ 	    && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
1e16b4b
+     {
1e16b4b
+ 	int i = ccline.xpc->xp_pattern - p;
1e16b4b
+ 
1e16b4b
+ 	/* If xp_pattern points inside the old cmdbuff it needs to be adjusted
1e16b4b
+ 	 * to point into the newly allocated memory. */
1e16b4b
+ 	if (i >= 0 && i <= ccline.cmdlen)
1e16b4b
+ 	    ccline.xpc->xp_pattern = ccline.cmdbuff + i;
1e16b4b
+     }
1e16b4b
+ 
1e16b4b
      return OK;
1e16b4b
  }
1e16b4b
  
1e16b4b
***************
1e16b4b
*** 2875,2880 ****
1e16b4b
--- 2899,2905 ----
1e16b4b
      prev_ccline = ccline;
1e16b4b
      ccline.cmdbuff = NULL;
1e16b4b
      ccline.cmdprompt = NULL;
1e16b4b
+     ccline.xpc = NULL;
1e16b4b
  }
1e16b4b
  
1e16b4b
  /*
1e16b4b
***************
1e16b4b
*** 3582,3587 ****
1e16b4b
--- 3607,3613 ----
1e16b4b
  ExpandInit(xp)
1e16b4b
      expand_T	*xp;
1e16b4b
  {
1e16b4b
+     xp->xp_pattern = NULL;
1e16b4b
      xp->xp_backslash = XP_BS_NONE;
1e16b4b
  #ifndef BACKSLASH_IN_FILENAME
1e16b4b
      xp->xp_shell = FALSE;
1e16b4b
*** ../vim-7.2.015/src/version.c	Wed Sep 10 18:25:18 2008
1e16b4b
--- src/version.c	Sun Sep 14 14:38:47 2008
1e16b4b
***************
1e16b4b
*** 678,679 ****
1e16b4b
--- 678,681 ----
1e16b4b
  {   /* Add new patch number below this line */
1e16b4b
+ /**/
1e16b4b
+     16,
1e16b4b
  /**/
1e16b4b
1e16b4b
-- 
1e16b4b
hundred-and-one symptoms of being an internet addict:
1e16b4b
53. To find out what time it is, you send yourself an e-mail and check the
1e16b4b
    "Date:" field.
1e16b4b
1e16b4b
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
1e16b4b
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
1e16b4b
\\\        download, build and distribute -- http://www.A-A-P.org        ///
1e16b4b
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///