]> Savannah Git Hosting - gnulib.git/commitdiff
mbiter, mbuiter, mbfile: Improve state handling after invalid input.
authorBruno Haible <bruno@clisp.org>
Tue, 4 Jul 2023 19:03:01 +0000 (21:03 +0200)
committerBruno Haible <bruno@clisp.org>
Tue, 4 Jul 2023 19:03:01 +0000 (21:03 +0200)
* lib/mbiter.h (mbiter_multi_next): After an invalid multibyte sequence
was encountered, clear the state.
* lib/mbuiter.h (mbuiter_multi_next): Likewise.
* lib/mbfile.h (mbfile_multi_getc): Likewise.

ChangeLog
lib/mbfile.h
lib/mbiter.h
lib/mbuiter.h

index c8709393b2b74e0c01072ef44af48d61f8839e67..83f0075f3b447f05b9f798494177bd4d8e174468 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2023-07-04  Bruno Haible  <bruno@clisp.org>
+
+       mbiter, mbuiter, mbfile: Improve state handling after invalid input.
+       * lib/mbiter.h (mbiter_multi_next): After an invalid multibyte sequence
+       was encountered, clear the state.
+       * lib/mbuiter.h (mbuiter_multi_next): Likewise.
+       * lib/mbfile.h (mbfile_multi_getc): Likewise.
+
 2023-07-04  Bruno Haible  <bruno@clisp.org>
 
        mbchar: Optimize all chars from the POSIX "portable character set".
index 9a2532992e9260005bbb85b9db260ffeec7a627b..7670dabfbb7a9912776209aa3e0a98de6d949522 100644 (file)
@@ -147,6 +147,8 @@ mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
           /* Return a single byte.  */
           bytes = 1;
           mbc->wc_valid = false;
+          /* Allow the next invocation to continue from a sane state.  */
+          memset (&mbf->state, '\0', sizeof (mbstate_t));
           break;
         }
       else if (bytes == (size_t) -2)
index bc88b4f3a20e7fd290b3ffc45fee9ac34a01be42..e4963010f3b9297d31941f44fae5258ec48c5f88 100644 (file)
@@ -146,8 +146,9 @@ mbiter_multi_next (struct mbiter_multi *iter)
           /* An invalid multibyte sequence was encountered.  */
           iter->cur.bytes = 1;
           iter->cur.wc_valid = false;
-          /* Whether to set iter->in_shift = false and reset iter->state
-             or not is not very important; the string is bogus anyway.  */
+          /* Allow the next invocation to continue from a sane state.  */
+          iter->in_shift = false;
+          memset (&iter->state, '\0', sizeof (mbstate_t));
         }
       else if (iter->cur.bytes == (size_t) -2)
         {
index 93dec816038d4058c7b108915fe3472f1731b019..e0fcd03d3d0d20d5a1d5a044159ef5b1476a4eee 100644 (file)
@@ -155,8 +155,9 @@ mbuiter_multi_next (struct mbuiter_multi *iter)
           /* An invalid multibyte sequence was encountered.  */
           iter->cur.bytes = 1;
           iter->cur.wc_valid = false;
-          /* Whether to set iter->in_shift = false and reset iter->state
-             or not is not very important; the string is bogus anyway.  */
+          /* Allow the next invocation to continue from a sane state.  */
+          iter->in_shift = false;
+          memset (&iter->state, '\0', sizeof (mbstate_t));
         }
       else if (iter->cur.bytes == (size_t) -2)
         {