81c43eb
To: vim-dev@vim.org
81c43eb
Subject: Patch 7.2.312
81c43eb
Fcc: outbox
81c43eb
From: Bram Moolenaar <Bram@moolenaar.net>
81c43eb
Mime-Version: 1.0
81c43eb
Content-Type: text/plain; charset=UTF-8
81c43eb
Content-Transfer-Encoding: 8bit
81c43eb
------------
81c43eb
81c43eb
Patch 7.2.312
81c43eb
Problem:    iconv() returns an invalid character sequence when conversion
81c43eb
	    fails.  It should return an empty string. (Yongwei Wu)
81c43eb
Solution:   Be more strict about invalid characters in the input.
81c43eb
Files:	    src/mbyte.c
81c43eb
81c43eb
81c43eb
*** ../vim-7.2.311/src/mbyte.c	2009-06-16 15:23:07.000000000 +0200
81c43eb
--- src/mbyte.c	2009-11-25 16:10:44.000000000 +0100
81c43eb
***************
81c43eb
*** 133,154 ****
81c43eb
  static int dbcs_ptr2cells_len __ARGS((char_u *p, int size));
81c43eb
  static int dbcs_ptr2char __ARGS((char_u *p));
81c43eb
  
81c43eb
! /* Lookup table to quickly get the length in bytes of a UTF-8 character from
81c43eb
!  * the first byte of a UTF-8 string.  Bytes which are illegal when used as the
81c43eb
!  * first byte have a one, because these will be used separately. */
81c43eb
  static char utf8len_tab[256] =
81c43eb
  {
81c43eb
      1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
      1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
      1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
      1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
!     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /*bogus*/
81c43eb
!     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /*bogus*/
81c43eb
      2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
81c43eb
      3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1,
81c43eb
  };
81c43eb
  
81c43eb
  /*
81c43eb
   * XIM often causes trouble.  Define XIM_DEBUG to get a log of XIM callbacks
81c43eb
   * in the "xim.log" file.
81c43eb
   */
81c43eb
--- 133,172 ----
81c43eb
  static int dbcs_ptr2cells_len __ARGS((char_u *p, int size));
81c43eb
  static int dbcs_ptr2char __ARGS((char_u *p));
81c43eb
  
81c43eb
! /*
81c43eb
!  * Lookup table to quickly get the length in bytes of a UTF-8 character from
81c43eb
!  * the first byte of a UTF-8 string.
81c43eb
!  * Bytes which are illegal when used as the first byte have a 1.
81c43eb
!  * The NUL byte has length 1.
81c43eb
!  */
81c43eb
  static char utf8len_tab[256] =
81c43eb
  {
81c43eb
      1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
      1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
      1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
      1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
!     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
!     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
      2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
81c43eb
      3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1,
81c43eb
  };
81c43eb
  
81c43eb
  /*
81c43eb
+  * Like utf8len_tab above, but using a zero for illegal lead bytes.
81c43eb
+  */
81c43eb
+ static char utf8len_tab_zero[256] =
81c43eb
+ {
81c43eb
+     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
+     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
+     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
+     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81c43eb
+     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
81c43eb
+     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
81c43eb
+     2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
81c43eb
+     3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,0,0,
81c43eb
+ };
81c43eb
+ 
81c43eb
+ /*
81c43eb
   * XIM often causes trouble.  Define XIM_DEBUG to get a log of XIM callbacks
81c43eb
   * in the "xim.log" file.
81c43eb
   */
81c43eb
***************
81c43eb
*** 1352,1358 ****
81c43eb
      if (size > 0 && *p >= 0x80)
81c43eb
      {
81c43eb
  	if (utf_ptr2len_len(p, size) < utf8len_tab[*p])
81c43eb
! 	    return 1;
81c43eb
  	c = utf_ptr2char(p);
81c43eb
  	/* An illegal byte is displayed as <xx>. */
81c43eb
  	if (utf_ptr2len(p) == 1 || c == NUL)
81c43eb
--- 1370,1376 ----
81c43eb
      if (size > 0 && *p >= 0x80)
81c43eb
      {
81c43eb
  	if (utf_ptr2len_len(p, size) < utf8len_tab[*p])
81c43eb
! 	    return 1;  /* truncated */
81c43eb
  	c = utf_ptr2char(p);
81c43eb
  	/* An illegal byte is displayed as <xx>. */
81c43eb
  	if (utf_ptr2len(p) == 1 || c == NUL)
81c43eb
***************
81c43eb
*** 1473,1479 ****
81c43eb
      if (p[0] < 0x80)	/* be quick for ASCII */
81c43eb
  	return p[0];
81c43eb
  
81c43eb
!     len = utf8len_tab[p[0]];
81c43eb
      if (len > 1 && (p[1] & 0xc0) == 0x80)
81c43eb
      {
81c43eb
  	if (len == 2)
81c43eb
--- 1491,1497 ----
81c43eb
      if (p[0] < 0x80)	/* be quick for ASCII */
81c43eb
  	return p[0];
81c43eb
  
81c43eb
!     len = utf8len_tab_zero[p[0]];
81c43eb
      if (len > 1 && (p[1] & 0xc0) == 0x80)
81c43eb
      {
81c43eb
  	if (len == 2)
81c43eb
***************
81c43eb
*** 1723,1728 ****
81c43eb
--- 1741,1747 ----
81c43eb
  /*
81c43eb
   * Return length of UTF-8 character, obtained from the first byte.
81c43eb
   * "b" must be between 0 and 255!
81c43eb
+  * Returns 1 for an invalid first byte value.
81c43eb
   */
81c43eb
      int
81c43eb
  utf_byte2len(b)
81c43eb
***************
81c43eb
*** 1737,1742 ****
81c43eb
--- 1756,1762 ----
81c43eb
   * Returns 1 for "".
81c43eb
   * Returns 1 for an illegal byte sequence (also in incomplete byte seq.).
81c43eb
   * Returns number > "size" for an incomplete byte sequence.
81c43eb
+  * Never returns zero.
81c43eb
   */
81c43eb
      int
81c43eb
  utf_ptr2len_len(p, size)
81c43eb
***************
81c43eb
*** 1747,1757 ****
81c43eb
      int		i;
81c43eb
      int		m;
81c43eb
  
81c43eb
!     if (*p == NUL)
81c43eb
! 	return 1;
81c43eb
!     m = len = utf8len_tab[*p];
81c43eb
      if (len > size)
81c43eb
  	m = size;	/* incomplete byte sequence. */
81c43eb
      for (i = 1; i < m; ++i)
81c43eb
  	if ((p[i] & 0xc0) != 0x80)
81c43eb
  	    return 1;
81c43eb
--- 1767,1779 ----
81c43eb
      int		i;
81c43eb
      int		m;
81c43eb
  
81c43eb
!     len = utf8len_tab[*p];
81c43eb
!     if (len == 1)
81c43eb
! 	return 1;	/* NUL, ascii or illegal lead byte */
81c43eb
      if (len > size)
81c43eb
  	m = size;	/* incomplete byte sequence. */
81c43eb
+     else
81c43eb
+ 	m = len;
81c43eb
      for (i = 1; i < m; ++i)
81c43eb
  	if ((p[i] & 0xc0) != 0x80)
81c43eb
  	    return 1;
81c43eb
***************
81c43eb
*** 2505,2510 ****
81c43eb
--- 2527,2533 ----
81c43eb
  /*
81c43eb
   * mb_head_off() function pointer.
81c43eb
   * Return offset from "p" to the first byte of the character it points into.
81c43eb
+  * If "p" points to the NUL at the end of the string return 0.
81c43eb
   * Returns 0 when already at the first byte of a character.
81c43eb
   */
81c43eb
      int
81c43eb
***************
81c43eb
*** 2524,2530 ****
81c43eb
  
81c43eb
      /* It can't be a trailing byte when not using DBCS, at the start of the
81c43eb
       * string or the previous byte can't start a double-byte. */
81c43eb
!     if (p <= base || MB_BYTE2LEN(p[-1]) == 1)
81c43eb
  	return 0;
81c43eb
  
81c43eb
      /* This is slow: need to start at the base and go forward until the
81c43eb
--- 2547,2553 ----
81c43eb
  
81c43eb
      /* It can't be a trailing byte when not using DBCS, at the start of the
81c43eb
       * string or the previous byte can't start a double-byte. */
81c43eb
!     if (p <= base || MB_BYTE2LEN(p[-1]) == 1 || *p == NUL)
81c43eb
  	return 0;
81c43eb
  
81c43eb
      /* This is slow: need to start at the base and go forward until the
81c43eb
***************
81c43eb
*** 2552,2558 ****
81c43eb
       * lead byte in the current cell. */
81c43eb
      if (p <= base
81c43eb
  	    || (enc_dbcs == DBCS_JPNU && p[-1] == 0x8e)
81c43eb
! 	    || MB_BYTE2LEN(p[-1]) == 1)
81c43eb
  	return 0;
81c43eb
  
81c43eb
      /* This is slow: need to start at the base and go forward until the
81c43eb
--- 2575,2582 ----
81c43eb
       * lead byte in the current cell. */
81c43eb
      if (p <= base
81c43eb
  	    || (enc_dbcs == DBCS_JPNU && p[-1] == 0x8e)
81c43eb
! 	    || MB_BYTE2LEN(p[-1]) == 1
81c43eb
! 	    || *p == NUL)
81c43eb
  	return 0;
81c43eb
  
81c43eb
      /* This is slow: need to start at the base and go forward until the
81c43eb
***************
81c43eb
*** 2578,2583 ****
81c43eb
--- 2602,2608 ----
81c43eb
      char_u	*q;
81c43eb
      char_u	*s;
81c43eb
      int		c;
81c43eb
+     int		len;
81c43eb
  #ifdef FEAT_ARABIC
81c43eb
      char_u	*j;
81c43eb
  #endif
81c43eb
***************
81c43eb
*** 2597,2604 ****
81c43eb
  	    --q;
81c43eb
  	/* Check for illegal sequence. Do allow an illegal byte after where we
81c43eb
  	 * started. */
81c43eb
! 	if (utf8len_tab[*q] != (int)(s - q + 1)
81c43eb
! 				       && utf8len_tab[*q] != (int)(p - q + 1))
81c43eb
  	    return 0;
81c43eb
  
81c43eb
  	if (q <= base)
81c43eb
--- 2622,2629 ----
81c43eb
  	    --q;
81c43eb
  	/* Check for illegal sequence. Do allow an illegal byte after where we
81c43eb
  	 * started. */
81c43eb
! 	len = utf8len_tab[*q];
81c43eb
! 	if (len != (int)(s - q + 1) && len != (int)(p - q + 1))
81c43eb
  	    return 0;
81c43eb
  
81c43eb
  	if (q <= base)
81c43eb
***************
81c43eb
*** 2810,2818 ****
81c43eb
  
81c43eb
      while (end == NULL ? *p != NUL : p < end)
81c43eb
      {
81c43eb
! 	if ((*p & 0xc0) == 0x80)
81c43eb
  	    return FALSE;	/* invalid lead byte */
81c43eb
- 	l = utf8len_tab[*p];
81c43eb
  	if (end != NULL && p + l > end)
81c43eb
  	    return FALSE;	/* incomplete byte sequence */
81c43eb
  	++p;
81c43eb
--- 2835,2843 ----
81c43eb
  
81c43eb
      while (end == NULL ? *p != NUL : p < end)
81c43eb
      {
81c43eb
! 	l = utf8len_tab_zero[*p];
81c43eb
! 	if (l == 0)
81c43eb
  	    return FALSE;	/* invalid lead byte */
81c43eb
  	if (end != NULL && p + l > end)
81c43eb
  	    return FALSE;	/* incomplete byte sequence */
81c43eb
  	++p;
81c43eb
***************
81c43eb
*** 6117,6128 ****
81c43eb
  	    d = retval;
81c43eb
  	    for (i = 0; i < len; ++i)
81c43eb
  	    {
81c43eb
! 		l = utf_ptr2len(ptr + i);
81c43eb
  		if (l == 0)
81c43eb
  		    *d++ = NUL;
81c43eb
  		else if (l == 1)
81c43eb
  		{
81c43eb
! 		    if (unconvlenp != NULL && utf8len_tab[ptr[i]] > len - i)
81c43eb
  		    {
81c43eb
  			/* Incomplete sequence at the end. */
81c43eb
  			*unconvlenp = len - i;
81c43eb
--- 6142,6161 ----
81c43eb
  	    d = retval;
81c43eb
  	    for (i = 0; i < len; ++i)
81c43eb
  	    {
81c43eb
! 		l = utf_ptr2len_len(ptr + i, len - i);
81c43eb
  		if (l == 0)
81c43eb
  		    *d++ = NUL;
81c43eb
  		else if (l == 1)
81c43eb
  		{
81c43eb
! 		    int l_w = utf8len_tab_zero[ptr[i]];
81c43eb
! 
81c43eb
! 		    if (l_w == 0)
81c43eb
! 		    {
81c43eb
! 			/* Illegal utf-8 byte cannot be converted */
81c43eb
! 			vim_free(retval);
81c43eb
! 			return NULL;
81c43eb
! 		    }
81c43eb
! 		    if (unconvlenp != NULL && l_w > len - i)
81c43eb
  		    {
81c43eb
  			/* Incomplete sequence at the end. */
81c43eb
  			*unconvlenp = len - i;
81c43eb
*** ../vim-7.2.311/src/version.c	2009-12-02 13:32:10.000000000 +0100
81c43eb
--- src/version.c	2009-12-02 15:00:23.000000000 +0100
81c43eb
***************
81c43eb
*** 683,684 ****
81c43eb
--- 683,686 ----
81c43eb
  {   /* Add new patch number below this line */
81c43eb
+ /**/
81c43eb
+     312,
81c43eb
  /**/
81c43eb
81c43eb
-- 
81c43eb
hundred-and-one symptoms of being an internet addict:
81c43eb
6. You refuse to go to a vacation spot with no electricity and no phone lines.
81c43eb
81c43eb
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
81c43eb
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
81c43eb
\\\        download, build and distribute -- http://www.A-A-P.org        ///
81c43eb
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///