From f4cb7c58da30c8dde304bb7c3b83af3ddba6c9c2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 14 Aug 2011 19:39:31 +0000 Subject: [PATCH] Fix return value of wxMBConvUTF8::ToWChar() when not using MAP_INVALID_UTF8_NOT. wxMBConvUTF8::ToWChar() was off by 1 when the input length was explicitly specified, the extra NUL should only be added in the implicit length case. This bug didn't occur for the default wxMBConvUTF8 object as it simply forwarded to the base class wxMBConvStrictUTF8 implementation but it happened when MAP_INVALID_UTF8_TO_OCTAL or MAP_INVALID_UTF8_TO_PUA was used. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@68694 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/strconv.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/common/strconv.cpp b/src/common/strconv.cpp index 170ad719bd..9c6299a5d3 100644 --- a/src/common/strconv.cpp +++ b/src/common/strconv.cpp @@ -1230,7 +1230,10 @@ size_t wxMBConvUTF8::ToWChar(wchar_t *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))) { const char *opsz = psz; bool invalid = false; @@ -1364,10 +1367,17 @@ size_t wxMBConvUTF8::ToWChar(wchar_t *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; - return len + 1; + // And count it in any case. + len++; + } + + return len; } static inline bool isoctal(wchar_t wch) -- 2.45.2