X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/6c4d607e60293cdd7e9774ad0a5af224882d91e5..87f0b1323b7ac77f02133b836c8dfee63b0fd387:/src/common/strconv.cpp diff --git a/src/common/strconv.cpp b/src/common/strconv.cpp index bf0b0923c7..73fe64d115 100644 --- a/src/common/strconv.cpp +++ b/src/common/strconv.cpp @@ -460,7 +460,6 @@ wxMBConv::cMB2WC(const char *inBuff, size_t inLen, size_t *outLen) const // because we want the buffer to always be NUL-terminated, even if the // input isn't (as otherwise the caller has no way to know its length) wxWCharBuffer wbuf(dstLen); - wbuf.data()[dstLen] = L'\0'; if ( ToWChar(wbuf.data(), dstLen, inBuff, inLen) != wxCONV_FAILED ) { if ( outLen ) @@ -995,7 +994,7 @@ wxMBConvStrictUTF8::ToWChar(wchar_t *dst, size_t dstLen, for ( const char *p = src; ; p++ ) { - if ( !(srcLen == wxNO_LEN ? *p : srcLen) ) + if ( (srcLen == wxNO_LEN ? !*p : !srcLen) ) { // all done successfully, just add the trailing NULL if we are not // using explicit length @@ -1115,7 +1114,7 @@ wxMBConvStrictUTF8::FromWChar(char *dst, size_t dstLen, for ( const wchar_t *wp = src; ; wp++ ) { - if ( !(srcLen == wxNO_LEN ? *wp : srcLen) ) + if ( (srcLen == wxNO_LEN ? !*wp : !srcLen) ) { // all done successfully, just add the trailing NULL if we are not // using explicit length @@ -1145,6 +1144,8 @@ wxMBConvStrictUTF8::FromWChar(char *dst, size_t dstLen, { // skip the next char too as we decoded a surrogate wp++; + if ( srcLen != wxNO_LEN ) + srcLen--; } #else // wchar_t is UTF-32 code = *wp & 0x7fffffff; @@ -1230,7 +1231,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 +1368,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; + + // And count it in any case. + len++; + } - return len + 1; + return len; } static inline bool isoctal(wchar_t wch) @@ -1383,7 +1394,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; @@ -1451,10 +1465,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; - return len + 1; + // And count it in any case. + len++; + } + + return len; } // ============================================================================ @@ -1645,7 +1666,7 @@ wxMBConvUTF16straight::FromWChar(char *dst, size_t dstLen, wxUint16 *outBuff = reinterpret_cast(dst); for ( size_t n = 0; n < srcLen; n++ ) { - wxUint16 cc[2]; + wxUint16 cc[2] = { 0 }; const size_t numChars = encode_utf16(*src++, cc); if ( numChars == wxCONV_FAILED ) return wxCONV_FAILED; @@ -1728,7 +1749,7 @@ wxMBConvUTF16swap::FromWChar(char *dst, size_t dstLen, wxUint16 *outBuff = reinterpret_cast(dst); for ( const wchar_t *srcEnd = src + srcLen; src < srcEnd; src++ ) { - wxUint16 cc[2]; + wxUint16 cc[2] = { 0 }; const size_t numChars = encode_utf16(*src, cc); if ( numChars == wxCONV_FAILED ) return wxCONV_FAILED; @@ -1812,7 +1833,7 @@ wxMBConvUTF32straight::ToWChar(wchar_t *dst, size_t dstLen, size_t outLen = 0; for ( size_t n = 0; n < inLen; n++ ) { - wxUint16 cc[2]; + wxUint16 cc[2] = { 0 }; const size_t numChars = encode_utf16(*inBuff++, cc); if ( numChars == wxCONV_FAILED ) return wxCONV_FAILED; @@ -1890,7 +1911,7 @@ wxMBConvUTF32swap::ToWChar(wchar_t *dst, size_t dstLen, size_t outLen = 0; for ( size_t n = 0; n < inLen; n++, inBuff++ ) { - wxUint16 cc[2]; + wxUint16 cc[2] = { 0 }; const size_t numChars = encode_utf16(wxUINT32_SWAP_ALWAYS(*inBuff), cc); if ( numChars == wxCONV_FAILED ) return wxCONV_FAILED; @@ -2183,7 +2204,7 @@ wxMBConv_iconv::wxMBConv_iconv(const char *name) { #if SIZEOF_WCHAR_T == 4 wxT("UCS-4"), -#elif SIZEOF_WCHAR_T = 2 +#elif SIZEOF_WCHAR_T == 2 wxT("UCS-2"), #endif NULL