]> git.eshelyaron.com Git - emacs.git/commitdiff
* 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 7ea9ad8f3e31260565f70a8fdcc5d5b0833ae55e..3acf12e4483651b851d87f2a616bb9daa74f41fb 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 df9ebc11f9c04d37c3aeaa2f9f298a1caaa1493a..b106ffb1938430a9ec02b016d57211f399a44ec9 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