https://gcc.gnu.org/bugzilla/show_bug.cgi?id=37475

--- Comment #11 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This changes the loop to always run if the input is non-empty, and so return
partial if the destination is empty.

--- a/libstdc++-v3/config/locale/gnu/codecvt_members.cc
+++ b/libstdc++-v3/config/locale/gnu/codecvt_members.cc
@@ -131,10 +131,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     // mbsnrtowcs is *very* fast but stops if encounters NUL characters:
     // in case we store a L'\0' and then continue, in a loop.
     // NB: mbsnrtowcs is a GNU extension
-    for (__from_next = __from, __to_next = __to;
-        __from_next < __from_end && __to_next < __to_end
-        && __ret == ok;)
+    __from_next = __from;
+    __to_next = __to;
+    while (__from_next < __from_end)
       {
+       if (__to_next >= __to_end)
+         {
+           __ret = partial;
+           break;
+         }
+
        const extern_type* __from_chunk_end;
        __from_chunk_end = static_cast<const extern_type*>(memchr(__from_next,
'\0',
                                                                  __from_end
@@ -162,12 +168,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
            __from_next = __from;
            __state = __tmp_state;
            __ret = error;
+           break;
          }
        else if (__from_next && __from_next < __from_chunk_end)
          {
            // It is unclear what to return in this case (see DR 382).
            __to_next += __conv;
            __ret = partial;
+           break;
          }
        else
          {
@@ -175,7 +183,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
            __to_next += __conv;
          }

-       if (__from_next < __from_end && __ret == ok)
+       if (__from_next < __from_end)
          {
            if (__to_next < __to_end)
              {
@@ -185,7 +193,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
                *__to_next++ = L'\0';
              }
            else
-             __ret = partial;
+             {
+               __ret = partial;
+               break;
+             }
          }
       }

Reply via email to