From: Vadim Zeitlin Date: Sat, 15 Sep 2007 00:36:16 +0000 (+0000) Subject: always NUL-terminate the buffers returned by cWC2MB/cMB2WC() overloads taking input... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/0dd13d21cd03499e1d8e86a22d0841b900bf2c8d always NUL-terminate the buffers returned by cWC2MB/cMB2WC() overloads taking input length, even if the input is not NUL-terminated (part of patch 1795174) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48703 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/common/strconv.cpp b/src/common/strconv.cpp index b74c577e2b..65f6cb7eee 100644 --- a/src/common/strconv.cpp +++ b/src/common/strconv.cpp @@ -391,7 +391,10 @@ wxMBConv::cMB2WC(const char *inBuff, size_t inLen, size_t *outLen) const const size_t dstLen = ToWChar(NULL, 0, inBuff, inLen); if ( dstLen != wxCONV_FAILED ) { - wxWCharBuffer wbuf(dstLen - 1); + // notice that we allocate space for dstLen+1 wide characters here + // 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); if ( ToWChar(wbuf.data(), dstLen, inBuff, inLen) != wxCONV_FAILED ) { if ( outLen ) @@ -417,16 +420,18 @@ wxMBConv::cWC2MB(const wchar_t *inBuff, size_t inLen, size_t *outLen) const size_t dstLen = FromWChar(NULL, 0, inBuff, inLen); if ( dstLen != wxCONV_FAILED ) { - // special case of empty input: can't allocate 0 size buffer below as - // wxCharBuffer insists on NUL-terminating it - wxCharBuffer buf(dstLen ? dstLen - 1 : 1); + const size_t nulLen = GetMBNulLen(); + + // as above, ensure that the buffer is always NUL-terminated, even if + // the input is not + wxCharBuffer buf(dstLen + nulLen - 1); + memset(buf.data() + dstLen, 0, nulLen); if ( FromWChar(buf.data(), dstLen, inBuff, inLen) != wxCONV_FAILED ) { if ( outLen ) { *outLen = dstLen; - const size_t nulLen = GetMBNulLen(); if ( dstLen >= nulLen && !NotAllNULs(buf.data() + dstLen - nulLen, nulLen) ) {