]> git.saurik.com Git - wxWidgets.git/commitdiff
Fix return value of wxMBConvUTF8::FromWChar().
authorVadim Zeitlin <vadim@wxwidgets.org>
Wed, 25 Jan 2012 00:10:44 +0000 (00:10 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Wed, 25 Jan 2012 00:10:44 +0000 (00:10 +0000)
Apply the same fix as was done in r68694 for ToWChar() to FromWChar(): it also
returned an off by 1 value when not using MAP_INVALID_UTF8_NOT.

Closes #13400.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@70462 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/common/strconv.cpp

index f6dbc341f5b31daff71766543cfaf4a6054a9c5e..54df1ff70ab2a94495b1257ca22c6a848b94185f 100644 (file)
@@ -1395,7 +1395,10 @@ size_t wxMBConvUTF8::FromWChar(char *buf, size_t n,
 
     size_t len = 0;
 
-    while ((srcLen == wxNO_LEN ? *psz : srcLen--) && ((!buf) || (len < n)))
+    // The length can be either given explicitly or computed implicitly for the
+    // NUL-terminated strings.
+    const bool isNulTerminated = srcLen == wxNO_LEN;
+    while ((isNulTerminated ? *psz : srcLen--) && ((!buf) || (len < n)))
     {
         wxUint32 cc;
 
@@ -1463,10 +1466,17 @@ size_t wxMBConvUTF8::FromWChar(char *buf, size_t n,
         }
     }
 
-    if (srcLen == wxNO_LEN && buf && (len < n))
-        *buf = 0;
+    if ( isNulTerminated )
+    {
+        // Add the trailing NUL in this case if we have a large enough buffer.
+        if ( buf && (len < n) )
+            *buf = 0;
+
+        // And count it in any case.
+        len++;
+    }
 
-    return len + 1;
+    return len;
 }
 
 // ============================================================================