* doprnt.c (doprnt): Truncate multibyte char correctly.
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 7 Jun 2012 05:11:51 +0000 (22:11 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 7 Jun 2012 05:11:51 +0000 (22:11 -0700)
Without this change, doprnt (buf, 2, "%s", FORMAT_END, AP)
would mishandle a string argument "Xc" if X was a multibyte
character of length 2: it would truncate after X's first byte
rather than including all of X.

src/ChangeLog
src/doprnt.c

index 7ea9ad8..3acf12e 100644 (file)
@@ -1,3 +1,11 @@
+2012-06-07  Paul Eggert  <eggert@cs.ucla.edu>
+
+       * doprnt.c (doprnt): Truncate multibyte char correctly.
+       Without this change, doprnt (buf, 2, "%s", FORMAT_END, AP)
+       would mishandle a string argument "Xc" if X was a multibyte
+       character of length 2: it would truncate after X's first byte
+       rather than including all of X.
+
 2012-06-06  Chong Yidong  <cyd@gnu.org>
 
        * buffer.c (word_wrap): Doc fix.
index df9ebc1..b106ffb 100644 (file)
@@ -392,15 +392,19 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
                {
                  /* Truncate the string at character boundary.  */
                  tem = bufsize;
-                 while (!CHAR_HEAD_P (string[tem - 1])) tem--;
-                 /* If the multibyte sequence of this character is
-                    too long for the space we have left in the
-                    buffer, truncate before it.  */
-                 if (tem > 0
-                     && BYTES_BY_CHAR_HEAD (string[tem - 1]) > bufsize)
-                   tem--;
-                 if (tem > 0)
-                   memcpy (bufptr, string, tem);
+                 do
+                   {
+                     tem--;
+                     if (CHAR_HEAD_P (string[tem]))
+                       {
+                         if (BYTES_BY_CHAR_HEAD (string[tem]) <= bufsize - tem)
+                           tem = bufsize;
+                         break;
+                       }
+                   }
+                 while (tem != 0);
+
+                 memcpy (bufptr, string, tem);
                  bufptr[tem] = 0;
                  /* Trigger exit from the loop, but make sure we
                     return to the caller a value which will indicate