X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/041e6050fdfa232472d7505b842d845d37dcb6de..f5406730ffdde05e710a68367ac45c15a11c00bc:/src/common/strconv.cpp diff --git a/src/common/strconv.cpp b/src/common/strconv.cpp index f6dbc341f5..54df1ff70a 100644 --- a/src/common/strconv.cpp +++ b/src/common/strconv.cpp @@ -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; } // ============================================================================