]> Savannah Git Hosting - gnulib.git/commitdiff
mbfile: Fix major bug (regression 2023-07-04).
authorBruno Haible <bruno@clisp.org>
Wed, 13 Sep 2023 14:23:35 +0000 (16:23 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 13 Sep 2023 14:23:35 +0000 (16:23 +0200)
Reported by Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org> in
<https://lists.gnu.org/archive/html/bug-gnulib/2023-09/msg00088.html>.

* lib/mbfile.h (mbfile_multi_getc): If bytes != -1, -2, -3, add the
previous mbf->bufcount to bytes.

ChangeLog
lib/mbfile.h

index ac57b7b5bc81e1c462e060e7d0f5368a14fe53b3..10be2e2aadfb8c9e34887f1277234000d3e6a235 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2023-09-13  Bruno Haible  <bruno@clisp.org>
+
+       mbfile: Fix major bug (regression 2023-07-04).
+       Reported by Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org> in
+       <https://lists.gnu.org/archive/html/bug-gnulib/2023-09/msg00088.html>.
+       * lib/mbfile.h (mbfile_multi_getc): If bytes != -1, -2, -3, add the
+       previous mbf->bufcount to bytes.
+
 2023-09-11  Pádraig Brady  <P@draigBrady.com>
 
        gnu-web-doc-update: fix updating of manual directory
index dccdedb03ca25bff5b7279b983412dc70302c690..ad61c19308743319a395768a65678df53e1762c1 100644 (file)
@@ -141,11 +141,11 @@ mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
     {
       /* Feed the bytes one by one into mbrtoc32.  */
       bytes = mbrtoc32 (&mbc->wc, &mbf->buf[mbf->bufcount], new_bufcount - mbf->bufcount, &mbf->state);
-      mbf->bufcount = new_bufcount;
 
       if (bytes == (size_t) -1)
         {
           /* An invalid multibyte sequence was encountered.  */
+          mbf->bufcount = new_bufcount;
           /* Return a single byte.  */
           bytes = 1;
           mbc->wc_valid = false;
@@ -156,6 +156,7 @@ mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
       else if (bytes == (size_t) -2)
         {
           /* An incomplete multibyte character.  */
+          mbf->bufcount = new_bufcount;
           if (mbf->bufcount == MBCHAR_BUF_SIZE)
             {
               /* An overlong incomplete multibyte sequence was encountered.  */
@@ -182,19 +183,27 @@ mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
         }
       else
         {
-          if (bytes == 0)
+          #if !GNULIB_MBRTOC32_REGULAR
+          if (bytes == (size_t) -3)
             {
-              /* A null 32-bit wide character was encountered.  */
-              bytes = 1;
-              assert (mbf->buf[0] == '\0');
-              assert (mbc->wc == 0);
+              /* The previous multibyte sequence produced an additional 32-bit
+                 wide character.  */
+              mbf->bufcount = new_bufcount;
+              bytes = 0;
             }
-          #if !GNULIB_MBRTOC32_REGULAR
-          else if (bytes == (size_t) -3)
-            /* The previous multibyte sequence produced an additional 32-bit
-               wide character.  */
-            bytes = 0;
+          else
           #endif
+            {
+              bytes = mbf->bufcount + bytes;
+              mbf->bufcount = new_bufcount;
+              if (bytes == 0)
+                {
+                  /* A null 32-bit wide character was encountered.  */
+                  bytes = 1;
+                  assert (mbf->buf[0] == '\0');
+                  assert (mbc->wc == 0);
+                }
+            }
           mbc->wc_valid = true;
           break;
         }