astepano / rpms / vim

Forked from rpms/vim 6 years ago
Clone
d172a1a
To: vim-dev@vim.org
d172a1a
Subject: Patch 7.2.080
d172a1a
Fcc: outbox
d172a1a
From: Bram Moolenaar <Bram@moolenaar.net>
d172a1a
Mime-Version: 1.0
d172a1a
Content-Type: text/plain; charset=ISO-8859-1
d172a1a
Content-Transfer-Encoding: 8bit
d172a1a
------------
d172a1a
d172a1a
Patch 7.2.080
d172a1a
Problem:    When typing a composing character just after starting completion
d172a1a
	    may access memory before its allocation point. (Dominique Pelle)
d172a1a
Solution:   Don't delete before the completion start column.  Add extra checks
d172a1a
	    for the offset not being negative.
d172a1a
Files:	    src/edit.c
d172a1a
d172a1a
d172a1a
*** ../vim-7.2.079/src/edit.c	Wed Aug  6 18:56:55 2008
d172a1a
--- src/edit.c	Tue Jan 13 12:05:57 2009
d172a1a
***************
d172a1a
*** 147,152 ****
d172a1a
--- 147,153 ----
d172a1a
  static int  ins_compl_bs __ARGS((void));
d172a1a
  static void ins_compl_new_leader __ARGS((void));
d172a1a
  static void ins_compl_addleader __ARGS((int c));
d172a1a
+ static int ins_compl_len __ARGS((void));
d172a1a
  static void ins_compl_restart __ARGS((void));
d172a1a
  static void ins_compl_set_original_text __ARGS((char_u *str));
d172a1a
  static void ins_compl_addfrommatch __ARGS((void));
d172a1a
***************
d172a1a
*** 197,203 ****
d172a1a
  static void mb_replace_pop_ins __ARGS((int cc));
d172a1a
  #endif
d172a1a
  static void replace_flush __ARGS((void));
d172a1a
! static void replace_do_bs __ARGS((void));
d172a1a
  #ifdef FEAT_CINDENT
d172a1a
  static int cindent_on __ARGS((void));
d172a1a
  #endif
d172a1a
--- 198,205 ----
d172a1a
  static void mb_replace_pop_ins __ARGS((int cc));
d172a1a
  #endif
d172a1a
  static void replace_flush __ARGS((void));
d172a1a
! static void replace_do_bs __ARGS((int limit_col));
d172a1a
! static int del_char_after_col __ARGS((int limit_col));
d172a1a
  #ifdef FEAT_CINDENT
d172a1a
  static int cindent_on __ARGS((void));
d172a1a
  #endif
d172a1a
***************
d172a1a
*** 1933,1938 ****
d172a1a
--- 1935,1942 ----
d172a1a
  /*
d172a1a
   * Backspace the cursor until the given column.  Handles REPLACE and VREPLACE
d172a1a
   * modes correctly.  May also be used when not in insert mode at all.
d172a1a
+  * Will attempt not to go before "col" even when there is a composing
d172a1a
+  * character.
d172a1a
   */
d172a1a
      void
d172a1a
  backspace_until_column(col)
d172a1a
***************
d172a1a
*** 1942,1954 ****
d172a1a
      {
d172a1a
  	curwin->w_cursor.col--;
d172a1a
  	if (State & REPLACE_FLAG)
d172a1a
! 	    replace_do_bs();
d172a1a
! 	else
d172a1a
! 	    (void)del_char(FALSE);
d172a1a
      }
d172a1a
  }
d172a1a
  #endif
d172a1a
  
d172a1a
  #if defined(FEAT_INS_EXPAND) || defined(PROTO)
d172a1a
  /*
d172a1a
   * CTRL-X pressed in Insert mode.
d172a1a
--- 1946,1994 ----
d172a1a
      {
d172a1a
  	curwin->w_cursor.col--;
d172a1a
  	if (State & REPLACE_FLAG)
d172a1a
! 	    replace_do_bs(col);
d172a1a
! 	else if (!del_char_after_col(col))
d172a1a
! 	    break;
d172a1a
      }
d172a1a
  }
d172a1a
  #endif
d172a1a
  
d172a1a
+ /*
d172a1a
+  * Like del_char(), but make sure not to go before column "limit_col".
d172a1a
+  * Only matters when there are composing characters.
d172a1a
+  * Return TRUE when something was deleted.
d172a1a
+  */
d172a1a
+    static int
d172a1a
+ del_char_after_col(limit_col)
d172a1a
+     int limit_col;
d172a1a
+ {
d172a1a
+ #ifdef FEAT_MBYTE
d172a1a
+     if (enc_utf8 && limit_col >= 0)
d172a1a
+     {
d172a1a
+ 	int ecol = curwin->w_cursor.col + 1;
d172a1a
+ 
d172a1a
+ 	/* Make sure the cursor is at the start of a character, but
d172a1a
+ 	 * skip forward again when going too far back because of a
d172a1a
+ 	 * composing character. */
d172a1a
+ 	mb_adjust_cursor();
d172a1a
+ 	while (curwin->w_cursor.col < limit_col)
d172a1a
+ 	{
d172a1a
+ 	    int l = utf_ptr2len(ml_get_cursor());
d172a1a
+ 
d172a1a
+ 	    if (l == 0)  /* end of line */
d172a1a
+ 		break;
d172a1a
+ 	    curwin->w_cursor.col += l;
d172a1a
+ 	}
d172a1a
+ 	if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
d172a1a
+ 	    return FALSE;
d172a1a
+ 	del_bytes((long)(ecol - curwin->w_cursor.col), FALSE, TRUE);
d172a1a
+     }
d172a1a
+     else
d172a1a
+ #endif
d172a1a
+ 	(void)del_char(FALSE);
d172a1a
+     return TRUE;
d172a1a
+ }
d172a1a
+ 
d172a1a
  #if defined(FEAT_INS_EXPAND) || defined(PROTO)
d172a1a
  /*
d172a1a
   * CTRL-X pressed in Insert mode.
d172a1a
***************
d172a1a
*** 2418,2424 ****
d172a1a
  	{
d172a1a
  	    had_match = (curwin->w_cursor.col > compl_col);
d172a1a
  	    ins_compl_delete();
d172a1a
! 	    ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
d172a1a
  	    ins_redraw(FALSE);
d172a1a
  
d172a1a
  	    /* When the match isn't there (to avoid matching itself) remove it
d172a1a
--- 2458,2464 ----
d172a1a
  	{
d172a1a
  	    had_match = (curwin->w_cursor.col > compl_col);
d172a1a
  	    ins_compl_delete();
d172a1a
! 	    ins_bytes(compl_leader + ins_compl_len());
d172a1a
  	    ins_redraw(FALSE);
d172a1a
  
d172a1a
  	    /* When the match isn't there (to avoid matching itself) remove it
d172a1a
***************
d172a1a
*** 2470,2476 ****
d172a1a
  	    *p = NUL;
d172a1a
  	    had_match = (curwin->w_cursor.col > compl_col);
d172a1a
  	    ins_compl_delete();
d172a1a
! 	    ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
d172a1a
  	    ins_redraw(FALSE);
d172a1a
  
d172a1a
  	    /* When the match isn't there (to avoid matching itself) remove it
d172a1a
--- 2510,2516 ----
d172a1a
  	    *p = NUL;
d172a1a
  	    had_match = (curwin->w_cursor.col > compl_col);
d172a1a
  	    ins_compl_delete();
d172a1a
! 	    ins_bytes(compl_leader + ins_compl_len());
d172a1a
  	    ins_redraw(FALSE);
d172a1a
  
d172a1a
  	    /* When the match isn't there (to avoid matching itself) remove it
d172a1a
***************
d172a1a
*** 3209,3215 ****
d172a1a
  {
d172a1a
      ins_compl_del_pum();
d172a1a
      ins_compl_delete();
d172a1a
!     ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
d172a1a
      compl_used_match = FALSE;
d172a1a
  
d172a1a
      if (compl_started)
d172a1a
--- 3249,3255 ----
d172a1a
  {
d172a1a
      ins_compl_del_pum();
d172a1a
      ins_compl_delete();
d172a1a
!     ins_bytes(compl_leader + ins_compl_len());
d172a1a
      compl_used_match = FALSE;
d172a1a
  
d172a1a
      if (compl_started)
d172a1a
***************
d172a1a
*** 3264,3269 ****
d172a1a
--- 3304,3323 ----
d172a1a
  }
d172a1a
  
d172a1a
  /*
d172a1a
+  * Return the length of the completion, from the completion start column to
d172a1a
+  * the cursor column.  Making sure it never goes below zero.
d172a1a
+  */
d172a1a
+     static int
d172a1a
+ ins_compl_len()
d172a1a
+ {
d172a1a
+     int off = curwin->w_cursor.col - compl_col;
d172a1a
+ 
d172a1a
+     if (off < 0)
d172a1a
+ 	return 0;
d172a1a
+     return off;
d172a1a
+ }
d172a1a
+ 
d172a1a
+ /*
d172a1a
   * Append one character to the match leader.  May reduce the number of
d172a1a
   * matches.
d172a1a
   */
d172a1a
***************
d172a1a
*** 3621,3630 ****
d172a1a
  	    {
d172a1a
  		ins_compl_delete();
d172a1a
  		if (compl_leader != NULL)
d172a1a
! 		    ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
d172a1a
  		else if (compl_first_match != NULL)
d172a1a
! 		    ins_bytes(compl_orig_text
d172a1a
! 					  + curwin->w_cursor.col - compl_col);
d172a1a
  		retval = TRUE;
d172a1a
  	    }
d172a1a
  
d172a1a
--- 3675,3683 ----
d172a1a
  	    {
d172a1a
  		ins_compl_delete();
d172a1a
  		if (compl_leader != NULL)
d172a1a
! 		    ins_bytes(compl_leader + ins_compl_len());
d172a1a
  		else if (compl_first_match != NULL)
d172a1a
! 		    ins_bytes(compl_orig_text + ins_compl_len());
d172a1a
  		retval = TRUE;
d172a1a
  	    }
d172a1a
  
d172a1a
***************
d172a1a
*** 4256,4262 ****
d172a1a
      static void
d172a1a
  ins_compl_insert()
d172a1a
  {
d172a1a
!     ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
d172a1a
      if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
d172a1a
  	compl_used_match = FALSE;
d172a1a
      else
d172a1a
--- 4309,4315 ----
d172a1a
      static void
d172a1a
  ins_compl_insert()
d172a1a
  {
d172a1a
!     ins_bytes(compl_shown_match->cp_str + ins_compl_len());
d172a1a
      if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
d172a1a
  	compl_used_match = FALSE;
d172a1a
      else
d172a1a
***************
d172a1a
*** 4425,4431 ****
d172a1a
  	if (!compl_get_longest || compl_used_match)
d172a1a
  	    ins_compl_insert();
d172a1a
  	else
d172a1a
! 	    ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
d172a1a
      }
d172a1a
      else
d172a1a
  	compl_used_match = FALSE;
d172a1a
--- 4478,4484 ----
d172a1a
  	if (!compl_get_longest || compl_used_match)
d172a1a
  	    ins_compl_insert();
d172a1a
  	else
d172a1a
! 	    ins_bytes(compl_leader + ins_compl_len());
d172a1a
      }
d172a1a
      else
d172a1a
  	compl_used_match = FALSE;
d172a1a
***************
d172a1a
*** 7123,7131 ****
d172a1a
   * cc == 0: character was inserted, delete it
d172a1a
   * cc > 0: character was replaced, put cc (first byte of original char) back
d172a1a
   * and check for more characters to be put back
d172a1a
   */
d172a1a
      static void
d172a1a
! replace_do_bs()
d172a1a
  {
d172a1a
      int		cc;
d172a1a
  #ifdef FEAT_VREPLACE
d172a1a
--- 7176,7187 ----
d172a1a
   * cc == 0: character was inserted, delete it
d172a1a
   * cc > 0: character was replaced, put cc (first byte of original char) back
d172a1a
   * and check for more characters to be put back
d172a1a
+  * When "limit_col" is >= 0, don't delete before this column.  Matters when
d172a1a
+  * using composing characters, use del_char_after_col() instead of del_char().
d172a1a
   */
d172a1a
      static void
d172a1a
! replace_do_bs(limit_col)
d172a1a
!     int		limit_col;
d172a1a
  {
d172a1a
      int		cc;
d172a1a
  #ifdef FEAT_VREPLACE
d172a1a
***************
d172a1a
*** 7153,7159 ****
d172a1a
  #ifdef FEAT_MBYTE
d172a1a
  	if (has_mbyte)
d172a1a
  	{
d172a1a
! 	    del_char(FALSE);
d172a1a
  # ifdef FEAT_VREPLACE
d172a1a
  	    if (State & VREPLACE_FLAG)
d172a1a
  		orig_len = (int)STRLEN(ml_get_cursor());
d172a1a
--- 7209,7215 ----
d172a1a
  #ifdef FEAT_MBYTE
d172a1a
  	if (has_mbyte)
d172a1a
  	{
d172a1a
! 	    (void)del_char_after_col(limit_col);
d172a1a
  # ifdef FEAT_VREPLACE
d172a1a
  	    if (State & VREPLACE_FLAG)
d172a1a
  		orig_len = (int)STRLEN(ml_get_cursor());
d172a1a
***************
d172a1a
*** 7203,7209 ****
d172a1a
  	changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
d172a1a
      }
d172a1a
      else if (cc == 0)
d172a1a
! 	(void)del_char(FALSE);
d172a1a
  }
d172a1a
  
d172a1a
  #ifdef FEAT_CINDENT
d172a1a
--- 7259,7265 ----
d172a1a
  	changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
d172a1a
      }
d172a1a
      else if (cc == 0)
d172a1a
! 	(void)del_char_after_col(limit_col);
d172a1a
  }
d172a1a
  
d172a1a
  #ifdef FEAT_CINDENT
d172a1a
***************
d172a1a
*** 8239,8245 ****
d172a1a
  	 * Replace mode */
d172a1a
  	if (curwin->w_cursor.lnum != Insstart.lnum
d172a1a
  		|| curwin->w_cursor.col >= Insstart.col)
d172a1a
! 	    replace_do_bs();
d172a1a
      }
d172a1a
      else
d172a1a
  	(void)del_char(FALSE);
d172a1a
--- 8295,8301 ----
d172a1a
  	 * Replace mode */
d172a1a
  	if (curwin->w_cursor.lnum != Insstart.lnum
d172a1a
  		|| curwin->w_cursor.col >= Insstart.col)
d172a1a
! 	    replace_do_bs(-1);
d172a1a
      }
d172a1a
      else
d172a1a
  	(void)del_char(FALSE);
d172a1a
***************
d172a1a
*** 8556,8562 ****
d172a1a
  		break;
d172a1a
  	    }
d172a1a
  	    if (State & REPLACE_FLAG)
d172a1a
! 		replace_do_bs();
d172a1a
  	    else
d172a1a
  	    {
d172a1a
  #ifdef FEAT_MBYTE
d172a1a
--- 8612,8618 ----
d172a1a
  		break;
d172a1a
  	    }
d172a1a
  	    if (State & REPLACE_FLAG)
d172a1a
! 		replace_do_bs(-1);
d172a1a
  	    else
d172a1a
  	    {
d172a1a
  #ifdef FEAT_MBYTE
d172a1a
*** ../vim-7.2.079/src/version.c	Tue Jan  6 16:13:42 2009
d172a1a
--- src/version.c	Tue Jan 13 12:25:29 2009
d172a1a
***************
d172a1a
*** 678,679 ****
d172a1a
--- 678,681 ----
d172a1a
  {   /* Add new patch number below this line */
d172a1a
+ /**/
d172a1a
+     80,
d172a1a
  /**/
d172a1a
d172a1a
-- 
d172a1a
At some point in the project somebody will start whining about the need to
d172a1a
determine the project "requirements".  This involves interviewing people who
d172a1a
don't know what they want but, curiously, know exactly when they need it.
d172a1a
				(Scott Adams - The Dilbert principle)
d172a1a
d172a1a
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
d172a1a
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
d172a1a
\\\        download, build and distribute -- http://www.A-A-P.org        ///
d172a1a
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///