Karsten Hopp 81c285
To: vim-dev@vim.org
Karsten Hopp 81c285
Subject: Patch 7.2.200
Karsten Hopp 81c285
Fcc: outbox
Karsten Hopp 81c285
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp 81c285
Mime-Version: 1.0
Karsten Hopp 81c285
Content-Type: text/plain; charset=UTF-8
Karsten Hopp 81c285
Content-Transfer-Encoding: 8bit
Karsten Hopp 81c285
------------
Karsten Hopp 81c285
Karsten Hopp 81c285
Patch 7.2.200
Karsten Hopp 81c285
Problem:    Reading past end of string when navigating the menu bar or
Karsten Hopp 81c285
	    resizing the window.
Karsten Hopp 81c285
Solution:   Add and use mb_ptr2len_len(). (partly by Dominique Pelle)
Karsten Hopp 81c285
	    Also add mb_ptr2cells_len() to prevent more trouble.
Karsten Hopp 81c285
Files:	    src/gui_gtk_x11.c, src/os_unix.c, src/globals.h, src/mbyte.c,
Karsten Hopp 81c285
	    src/proto/mbyte.pro
Karsten Hopp 81c285
Karsten Hopp 81c285
Karsten Hopp 81c285
*** ../vim-7.2.199/src/gui_gtk_x11.c	2009-06-03 16:20:09.000000000 +0200
Karsten Hopp 81c285
--- src/gui_gtk_x11.c	2009-06-16 14:44:19.000000000 +0200
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 6077,6088 ****
Karsten Hopp 81c285
  # ifdef FEAT_MBYTE
Karsten Hopp 81c285
  	    if (enc_utf8)
Karsten Hopp 81c285
  	    {
Karsten Hopp 81c285
! 		c = utf_ptr2char(p);
Karsten Hopp 81c285
  		if (c >= 0x10000)	/* show chars > 0xffff as ? */
Karsten Hopp 81c285
  		    c = 0xbf;
Karsten Hopp 81c285
  		buf[textlen].byte1 = c >> 8;
Karsten Hopp 81c285
  		buf[textlen].byte2 = c;
Karsten Hopp 81c285
! 		p += utf_ptr2len(p);
Karsten Hopp 81c285
  		width += utf_char2cells(c);
Karsten Hopp 81c285
  	    }
Karsten Hopp 81c285
  	    else
Karsten Hopp 81c285
--- 6135,6149 ----
Karsten Hopp 81c285
  # ifdef FEAT_MBYTE
Karsten Hopp 81c285
  	    if (enc_utf8)
Karsten Hopp 81c285
  	    {
Karsten Hopp 81c285
! 		int pcc[MAX_MCO];
Karsten Hopp 81c285
! 
Karsten Hopp 81c285
! 		/* TODO: use the composing characters */
Karsten Hopp 81c285
! 		c = utfc_ptr2char_len(p, &pcc, len - (p - s));
Karsten Hopp 81c285
  		if (c >= 0x10000)	/* show chars > 0xffff as ? */
Karsten Hopp 81c285
  		    c = 0xbf;
Karsten Hopp 81c285
  		buf[textlen].byte1 = c >> 8;
Karsten Hopp 81c285
  		buf[textlen].byte2 = c;
Karsten Hopp 81c285
! 		p += utfc_ptr2len_len(p, len - (p - s));
Karsten Hopp 81c285
  		width += utf_char2cells(c);
Karsten Hopp 81c285
  	    }
Karsten Hopp 81c285
  	    else
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 6106,6113 ****
Karsten Hopp 81c285
  	if (has_mbyte)
Karsten Hopp 81c285
  	{
Karsten Hopp 81c285
  	    width = 0;
Karsten Hopp 81c285
! 	    for (p = s; p < s + len; p += (*mb_ptr2len)(p))
Karsten Hopp 81c285
! 		width += (*mb_ptr2cells)(p);
Karsten Hopp 81c285
  	}
Karsten Hopp 81c285
  	else
Karsten Hopp 81c285
  # endif
Karsten Hopp 81c285
--- 6167,6174 ----
Karsten Hopp 81c285
  	if (has_mbyte)
Karsten Hopp 81c285
  	{
Karsten Hopp 81c285
  	    width = 0;
Karsten Hopp 81c285
! 	    for (p = s; p < s + len; p += (*mb_ptr2len_len)(p, len - (p - s)))
Karsten Hopp 81c285
! 		width += (*mb_ptr2cells_len)(p, len - (p - s));
Karsten Hopp 81c285
  	}
Karsten Hopp 81c285
  	else
Karsten Hopp 81c285
  # endif
Karsten Hopp 81c285
*** ../vim-7.2.199/src/os_unix.c	2009-05-17 13:30:58.000000000 +0200
Karsten Hopp 81c285
--- src/os_unix.c	2009-06-03 12:35:59.000000000 +0200
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 4305,4311 ****
Karsten Hopp 81c285
  				ta_buf[i] = '\n';
Karsten Hopp 81c285
  # ifdef FEAT_MBYTE
Karsten Hopp 81c285
  			    if (has_mbyte)
Karsten Hopp 81c285
! 				i += (*mb_ptr2len)(ta_buf + i) - 1;
Karsten Hopp 81c285
  # endif
Karsten Hopp 81c285
  			}
Karsten Hopp 81c285
  
Karsten Hopp 81c285
--- 4305,4312 ----
Karsten Hopp 81c285
  				ta_buf[i] = '\n';
Karsten Hopp 81c285
  # ifdef FEAT_MBYTE
Karsten Hopp 81c285
  			    if (has_mbyte)
Karsten Hopp 81c285
! 				i += (*mb_ptr2len_len)(ta_buf + i,
Karsten Hopp 81c285
! 							ta_len + len - i) - 1;
Karsten Hopp 81c285
  # endif
Karsten Hopp 81c285
  			}
Karsten Hopp 81c285
  
Karsten Hopp 81c285
*** ../vim-7.2.199/src/globals.h	2009-06-10 18:15:49.000000000 +0200
Karsten Hopp 81c285
--- src/globals.h	2009-06-12 21:10:30.000000000 +0200
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 810,820 ****
Karsten Hopp 81c285
--- 815,828 ----
Karsten Hopp 81c285
   */
Karsten Hopp 81c285
  /* length of char in bytes, including following composing chars */
Karsten Hopp 81c285
  EXTERN int (*mb_ptr2len) __ARGS((char_u *p)) INIT(= latin_ptr2len);
Karsten Hopp 81c285
+ /* idem, with limit on string length */
Karsten Hopp 81c285
+ EXTERN int (*mb_ptr2len_len) __ARGS((char_u *p, int size)) INIT(= latin_ptr2len_len);
Karsten Hopp 81c285
  /* byte length of char */
Karsten Hopp 81c285
  EXTERN int (*mb_char2len) __ARGS((int c)) INIT(= latin_char2len);
Karsten Hopp 81c285
  /* convert char to bytes, return the length */
Karsten Hopp 81c285
  EXTERN int (*mb_char2bytes) __ARGS((int c, char_u *buf)) INIT(= latin_char2bytes);
Karsten Hopp 81c285
  EXTERN int (*mb_ptr2cells) __ARGS((char_u *p)) INIT(= latin_ptr2cells);
Karsten Hopp 81c285
+ EXTERN int (*mb_ptr2cells_len) __ARGS((char_u *p, int size)) INIT(= latin_ptr2cells_len);
Karsten Hopp 81c285
  EXTERN int (*mb_char2cells) __ARGS((int c)) INIT(= latin_char2cells);
Karsten Hopp 81c285
  EXTERN int (*mb_off2cells) __ARGS((unsigned off, unsigned max_off)) INIT(= latin_off2cells);
Karsten Hopp 81c285
  EXTERN int (*mb_ptr2char) __ARGS((char_u *p)) INIT(= latin_ptr2char);
Karsten Hopp 81c285
*** ../vim-7.2.199/src/mbyte.c	2009-05-17 13:30:58.000000000 +0200
Karsten Hopp 81c285
--- src/mbyte.c	2009-06-16 15:01:30.000000000 +0200
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 127,133 ****
Karsten Hopp 81c285
--- 127,136 ----
Karsten Hopp 81c285
  static int dbcs_char2len __ARGS((int c));
Karsten Hopp 81c285
  static int dbcs_char2bytes __ARGS((int c, char_u *buf));
Karsten Hopp 81c285
  static int dbcs_ptr2len __ARGS((char_u *p));
Karsten Hopp 81c285
+ static int dbcs_ptr2len_len __ARGS((char_u *p, int size));
Karsten Hopp 81c285
+ static int utf_ptr2cells_len __ARGS((char_u *p, int size));
Karsten Hopp 81c285
  static int dbcs_char2cells __ARGS((int c));
Karsten Hopp 81c285
+ static int dbcs_ptr2cells_len __ARGS((char_u *p, int size));
Karsten Hopp 81c285
  static int dbcs_ptr2char __ARGS((char_u *p));
Karsten Hopp 81c285
  
Karsten Hopp 81c285
  /* Lookup table to quickly get the length in bytes of a UTF-8 character from
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 606,614 ****
Karsten Hopp 81c285
--- 609,619 ----
Karsten Hopp 81c285
      if (enc_utf8)
Karsten Hopp 81c285
      {
Karsten Hopp 81c285
  	mb_ptr2len = utfc_ptr2len;
Karsten Hopp 81c285
+ 	mb_ptr2len_len = utfc_ptr2len_len;
Karsten Hopp 81c285
  	mb_char2len = utf_char2len;
Karsten Hopp 81c285
  	mb_char2bytes = utf_char2bytes;
Karsten Hopp 81c285
  	mb_ptr2cells = utf_ptr2cells;
Karsten Hopp 81c285
+ 	mb_ptr2cells_len = utf_ptr2cells_len;
Karsten Hopp 81c285
  	mb_char2cells = utf_char2cells;
Karsten Hopp 81c285
  	mb_off2cells = utf_off2cells;
Karsten Hopp 81c285
  	mb_ptr2char = utf_ptr2char;
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 617,625 ****
Karsten Hopp 81c285
--- 622,632 ----
Karsten Hopp 81c285
      else if (enc_dbcs != 0)
Karsten Hopp 81c285
      {
Karsten Hopp 81c285
  	mb_ptr2len = dbcs_ptr2len;
Karsten Hopp 81c285
+ 	mb_ptr2len_len = dbcs_ptr2len_len;
Karsten Hopp 81c285
  	mb_char2len = dbcs_char2len;
Karsten Hopp 81c285
  	mb_char2bytes = dbcs_char2bytes;
Karsten Hopp 81c285
  	mb_ptr2cells = dbcs_ptr2cells;
Karsten Hopp 81c285
+ 	mb_ptr2cells_len = dbcs_ptr2cells_len;
Karsten Hopp 81c285
  	mb_char2cells = dbcs_char2cells;
Karsten Hopp 81c285
  	mb_off2cells = dbcs_off2cells;
Karsten Hopp 81c285
  	mb_ptr2char = dbcs_ptr2char;
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 628,636 ****
Karsten Hopp 81c285
--- 635,645 ----
Karsten Hopp 81c285
      else
Karsten Hopp 81c285
      {
Karsten Hopp 81c285
  	mb_ptr2len = latin_ptr2len;
Karsten Hopp 81c285
+ 	mb_ptr2len_len = latin_ptr2len_len;
Karsten Hopp 81c285
  	mb_char2len = latin_char2len;
Karsten Hopp 81c285
  	mb_char2bytes = latin_char2bytes;
Karsten Hopp 81c285
  	mb_ptr2cells = latin_ptr2cells;
Karsten Hopp 81c285
+ 	mb_ptr2cells_len = latin_ptr2cells_len;
Karsten Hopp 81c285
  	mb_char2cells = latin_char2cells;
Karsten Hopp 81c285
  	mb_off2cells = latin_off2cells;
Karsten Hopp 81c285
  	mb_ptr2char = latin_ptr2char;
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 1069,1075 ****
Karsten Hopp 81c285
   * Get byte length of character at "*p" but stop at a NUL.
Karsten Hopp 81c285
   * For UTF-8 this includes following composing characters.
Karsten Hopp 81c285
   * Returns 0 when *p is NUL.
Karsten Hopp 81c285
-  *
Karsten Hopp 81c285
   */
Karsten Hopp 81c285
      int
Karsten Hopp 81c285
  latin_ptr2len(p)
Karsten Hopp 81c285
--- 1078,1083 ----
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 1091,1096 ****
Karsten Hopp 81c285
--- 1099,1138 ----
Karsten Hopp 81c285
      return len;
Karsten Hopp 81c285
  }
Karsten Hopp 81c285
  
Karsten Hopp 81c285
+ /*
Karsten Hopp 81c285
+  * mb_ptr2len_len() function pointer.
Karsten Hopp 81c285
+  * Like mb_ptr2len(), but limit to read "size" bytes.
Karsten Hopp 81c285
+  * Returns 0 for an empty string.
Karsten Hopp 81c285
+  * Returns 1 for an illegal char or an incomplete byte sequence.
Karsten Hopp 81c285
+  */
Karsten Hopp 81c285
+     int
Karsten Hopp 81c285
+ latin_ptr2len_len(p, size)
Karsten Hopp 81c285
+     char_u	*p;
Karsten Hopp 81c285
+     int		size;
Karsten Hopp 81c285
+ {
Karsten Hopp 81c285
+     if (size < 1 || *p == NUL)
Karsten Hopp 81c285
+ 	return 0;
Karsten Hopp 81c285
+     return 1;
Karsten Hopp 81c285
+ }
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
+     static int
Karsten Hopp 81c285
+ dbcs_ptr2len_len(p, size)
Karsten Hopp 81c285
+     char_u	*p;
Karsten Hopp 81c285
+     int		size;
Karsten Hopp 81c285
+ {
Karsten Hopp 81c285
+     int		len;
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
+     if (size < 1 || *p == NUL)
Karsten Hopp 81c285
+ 	return 0;
Karsten Hopp 81c285
+     if (size == 1)
Karsten Hopp 81c285
+ 	return 1;
Karsten Hopp 81c285
+     /* Check that second byte is not missing. */
Karsten Hopp 81c285
+     len = MB_BYTE2LEN(*p);
Karsten Hopp 81c285
+     if (len == 2 && p[1] == NUL)
Karsten Hopp 81c285
+ 	len = 1;
Karsten Hopp 81c285
+     return len;
Karsten Hopp 81c285
+ }
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
  struct interval
Karsten Hopp 81c285
  {
Karsten Hopp 81c285
      unsigned short first;
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 1287,1292 ****
Karsten Hopp 81c285
--- 1329,1383 ----
Karsten Hopp 81c285
  }
Karsten Hopp 81c285
  
Karsten Hopp 81c285
  /*
Karsten Hopp 81c285
+  * mb_ptr2cells_len() function pointer.
Karsten Hopp 81c285
+  * Like mb_ptr2cells(), but limit string length to "size".
Karsten Hopp 81c285
+  * For an empty string or truncated character returns 1.
Karsten Hopp 81c285
+  */
Karsten Hopp 81c285
+     int
Karsten Hopp 81c285
+ latin_ptr2cells_len(p, size)
Karsten Hopp 81c285
+     char_u	*p UNUSED;
Karsten Hopp 81c285
+     int		size UNUSED;
Karsten Hopp 81c285
+ {
Karsten Hopp 81c285
+     return 1;
Karsten Hopp 81c285
+ }
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
+     static int
Karsten Hopp 81c285
+ utf_ptr2cells_len(p, size)
Karsten Hopp 81c285
+     char_u	*p;
Karsten Hopp 81c285
+     int		size;
Karsten Hopp 81c285
+ {
Karsten Hopp 81c285
+     int		c;
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
+     /* Need to convert to a wide character. */
Karsten Hopp 81c285
+     if (size > 0 && *p >= 0x80)
Karsten Hopp 81c285
+     {
Karsten Hopp 81c285
+ 	if (utf_ptr2len_len(p, size) < utf8len_tab[*p])
Karsten Hopp 81c285
+ 	    return 1;
Karsten Hopp 81c285
+ 	c = utf_ptr2char(p);
Karsten Hopp 81c285
+ 	/* An illegal byte is displayed as <xx>. */
Karsten Hopp 81c285
+ 	if (utf_ptr2len(p) == 1 || c == NUL)
Karsten Hopp 81c285
+ 	    return 4;
Karsten Hopp 81c285
+ 	/* If the char is ASCII it must be an overlong sequence. */
Karsten Hopp 81c285
+ 	if (c < 0x80)
Karsten Hopp 81c285
+ 	    return char2cells(c);
Karsten Hopp 81c285
+ 	return utf_char2cells(c);
Karsten Hopp 81c285
+     }
Karsten Hopp 81c285
+     return 1;
Karsten Hopp 81c285
+ }
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
+     static int
Karsten Hopp 81c285
+ dbcs_ptr2cells_len(p, size)
Karsten Hopp 81c285
+     char_u	*p;
Karsten Hopp 81c285
+     int		size;
Karsten Hopp 81c285
+ {
Karsten Hopp 81c285
+     /* Number of cells is equal to number of bytes, except for euc-jp when
Karsten Hopp 81c285
+      * the first byte is 0x8e. */
Karsten Hopp 81c285
+     if (size <= 1 || (enc_dbcs == DBCS_JPNU && *p == 0x8e))
Karsten Hopp 81c285
+ 	return 1;
Karsten Hopp 81c285
+     return MB_BYTE2LEN(*p);
Karsten Hopp 81c285
+ }
Karsten Hopp 81c285
+ 
Karsten Hopp 81c285
+ /*
Karsten Hopp 81c285
   * mb_char2cells() function pointer.
Karsten Hopp 81c285
   * Return the number of display cells character "c" occupies.
Karsten Hopp 81c285
   * Only takes care of multi-byte chars, not "^C" and such.
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 1716,1721 ****
Karsten Hopp 81c285
--- 1807,1813 ----
Karsten Hopp 81c285
  /*
Karsten Hopp 81c285
   * Return the number of bytes the UTF-8 encoding of the character at "p[size]"
Karsten Hopp 81c285
   * takes.  This includes following composing characters.
Karsten Hopp 81c285
+  * Returns 0 for an empty string.
Karsten Hopp 81c285
   * Returns 1 for an illegal char or an incomplete byte sequence.
Karsten Hopp 81c285
   */
Karsten Hopp 81c285
      int
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 1728,1734 ****
Karsten Hopp 81c285
      int		prevlen;
Karsten Hopp 81c285
  #endif
Karsten Hopp 81c285
  
Karsten Hopp 81c285
!     if (*p == NUL)
Karsten Hopp 81c285
  	return 0;
Karsten Hopp 81c285
      if (p[0] < 0x80 && (size == 1 || p[1] < 0x80)) /* be quick for ASCII */
Karsten Hopp 81c285
  	return 1;
Karsten Hopp 81c285
--- 1820,1826 ----
Karsten Hopp 81c285
      int		prevlen;
Karsten Hopp 81c285
  #endif
Karsten Hopp 81c285
  
Karsten Hopp 81c285
!     if (size < 1 || *p == NUL)
Karsten Hopp 81c285
  	return 0;
Karsten Hopp 81c285
      if (p[0] < 0x80 && (size == 1 || p[1] < 0x80)) /* be quick for ASCII */
Karsten Hopp 81c285
  	return 1;
Karsten Hopp 81c285
*** ../vim-7.2.199/src/proto/mbyte.pro	2008-07-13 19:34:19.000000000 +0200
Karsten Hopp 81c285
--- src/proto/mbyte.pro	2009-06-16 14:58:39.000000000 +0200
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 7,16 ****
Karsten Hopp 81c285
--- 7,18 ----
Karsten Hopp 81c285
  int latin_char2len __ARGS((int c));
Karsten Hopp 81c285
  int latin_char2bytes __ARGS((int c, char_u *buf));
Karsten Hopp 81c285
  int latin_ptr2len __ARGS((char_u *p));
Karsten Hopp 81c285
+ int latin_ptr2len_len __ARGS((char_u *p, int size));
Karsten Hopp 81c285
  int utf_char2cells __ARGS((int c));
Karsten Hopp 81c285
  int latin_ptr2cells __ARGS((char_u *p));
Karsten Hopp 81c285
  int utf_ptr2cells __ARGS((char_u *p));
Karsten Hopp 81c285
  int dbcs_ptr2cells __ARGS((char_u *p));
Karsten Hopp 81c285
+ int latin_ptr2cells_len __ARGS((char_u *p, int size));
Karsten Hopp 81c285
  int latin_char2cells __ARGS((int c));
Karsten Hopp 81c285
  int latin_off2cells __ARGS((unsigned off, unsigned max_off));
Karsten Hopp 81c285
  int dbcs_off2cells __ARGS((unsigned off, unsigned max_off));
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 85,90 ****
Karsten Hopp 81c285
--- 87,93 ----
Karsten Hopp 81c285
  int preedit_get_status __ARGS((void));
Karsten Hopp 81c285
  int im_is_preediting __ARGS((void));
Karsten Hopp 81c285
  int convert_setup __ARGS((vimconv_T *vcp, char_u *from, char_u *to));
Karsten Hopp 81c285
+ int convert_setup_ext __ARGS((vimconv_T *vcp, char_u *from, int from_unicode_is_utf8, char_u *to, int to_unicode_is_utf8));
Karsten Hopp 81c285
  int convert_input __ARGS((char_u *ptr, int len, int maxlen));
Karsten Hopp 81c285
  int convert_input_safe __ARGS((char_u *ptr, int len, int maxlen, char_u **restp, int *restlenp));
Karsten Hopp 81c285
  char_u *string_convert __ARGS((vimconv_T *vcp, char_u *ptr, int *lenp));
Karsten Hopp 81c285
*** ../vim-7.2.199/src/version.c	2009-06-16 14:31:56.000000000 +0200
Karsten Hopp 81c285
--- src/version.c	2009-06-16 14:37:38.000000000 +0200
Karsten Hopp 81c285
***************
Karsten Hopp 81c285
*** 678,679 ****
Karsten Hopp 81c285
--- 678,681 ----
Karsten Hopp 81c285
  {   /* Add new patch number below this line */
Karsten Hopp 81c285
+ /**/
Karsten Hopp 81c285
+     200,
Karsten Hopp 81c285
  /**/
Karsten Hopp 81c285
Karsten Hopp 81c285
-- 
Karsten Hopp 81c285
How To Keep A Healthy Level Of Insanity:
Karsten Hopp 81c285
12. Sing along at the opera.
Karsten Hopp 81c285
Karsten Hopp 81c285
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp 81c285
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp 81c285
\\\        download, build and distribute -- http://www.A-A-P.org        ///
Karsten Hopp 81c285
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///