From 37dd89a0da3ab22c2720d40a595134c0ab4b9978 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 13 Nov 2015 19:08:27 +0100 Subject: [PATCH] Don't rely on wxMBConv::cWC2MB(NULL) returning the exact byte count UTF-32 conversions use a useful optimization by avoiding the extra scan of the input wchar_t string in their FromWChar() and cWC2MB() implementation when they are only asked to compute the required buffer size without actually doing the conversion. However this means that for an input string containing UTF-16 surrogates (which is possible under MSW where wchar_t is 16 bits) the actual size of the output string can be smaller than that returned by FromWChar(NULL). Document that this may happen and avoid relying on the exact equality in the tests. See #17070. --- interface/wx/strconv.h | 11 ++++++++--- tests/mbconv/mbconvtest.cpp | 7 ++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/interface/wx/strconv.h b/interface/wx/strconv.h index 9d89c37e78..9388b49135 100644 --- a/interface/wx/strconv.h +++ b/interface/wx/strconv.h @@ -121,7 +121,7 @@ public: including the terminating @c NUL character(s). @return - The number of character written (or which would have been written + The number of characters written (or which would have been written if it were non-@NULL) to @a dst or @c wxCONV_FAILED on error. */ virtual size_t ToWChar(wchar_t* dst, size_t dstLen, const char* src, @@ -148,8 +148,13 @@ public: including the terminating @c NUL character. @return - The number of character written (or which would have been written - if it were non-@NULL) to @a dst or @c wxCONV_FAILED on error. + If @dst is non-@NULL, the number of characters actually written to + it. If @dst is @NULL, the returned value is at least equal to the + number of characters that would have been written out if it were + non-@NULL, but can be larger than it under the platforms using + UTF-16 as @c wchar_t encoding (this allows a useful optimization in + the implementation of this function for UTF-32). In any case, + @c wxCONV_FAILED is returned on conversion error. */ virtual size_t FromWChar(char* dst, size_t dstLen, const wchar_t* src, size_t srcLen = wxNO_LEN) const; diff --git a/tests/mbconv/mbconvtest.cpp b/tests/mbconv/mbconvtest.cpp index 10985fe3d5..69044cc057 100644 --- a/tests/mbconv/mbconvtest.cpp +++ b/tests/mbconv/mbconvtest.cpp @@ -1096,15 +1096,16 @@ void MBConvTestCase::TestEncoder( memcpy( inputCopy.data(), wideBuffer, (wideChars*sizeof(wchar_t)) ); inputCopy.data()[wideChars] = 0; - // calculate the output size + // calculate the output size: notice that it can be greater than the real + // size as the converter is allowed to estimate the maximal size needed + // instead of computing it precisely size_t outputWritten = converter.WC2MB ( 0, (const wchar_t*)inputCopy.data(), 0 ); - // make sure the correct output length was calculated - CPPUNIT_ASSERT_EQUAL( multiBytes, outputWritten ); + CPPUNIT_ASSERT( outputWritten >= multiBytes ); // convert the string size_t guardBytes = 8; // to make sure we're not overrunning the output buffer