From: Vadim Zeitlin Date: Sun, 24 Jan 2010 10:13:40 +0000 (+0000) Subject: Correct bug with returning 0 for non-empty input from wxConvAuto::ToWChar(). X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/4ca973967a5199ef46f425b8bc552fb19d433aba Correct bug with returning 0 for non-empty input from wxConvAuto::ToWChar(). Since the changes of r63064 we could return 0 when asked to convert a non-empty buffer containing only a BOM. This confused the logic in wxTextInputStream::NextChar() and was generally unexpected so now return wxCONV_FAILED in this case. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63245 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/convauto.h b/include/wx/convauto.h index 3a2e2e4809..4de2bee9e7 100644 --- a/include/wx/convauto.h +++ b/include/wx/convauto.h @@ -107,11 +107,11 @@ private: void InitFromBOM(BOMType bomType); // create the correct conversion object for the BOM present in the - // beginning of the buffer; adjust the buffer to skip the BOM if found + // beginning of the buffer // // return false if the buffer is too short to allow us to determine if we // have BOM or not - bool InitFromInput(const char **src, size_t *len); + bool InitFromInput(const char *src, size_t len); // adjust src and len to skip over the BOM (identified by m_bomType) at the // start of the buffer diff --git a/src/common/convauto.cpp b/src/common/convauto.cpp index c9ff7df9f6..c684613f4c 100644 --- a/src/common/convauto.cpp +++ b/src/common/convauto.cpp @@ -227,14 +227,13 @@ void wxConvAuto::SkipBOM(const char **src, size_t *len) const *len -= ofs; } -bool wxConvAuto::InitFromInput(const char **src, size_t *len) +bool wxConvAuto::InitFromInput(const char *src, size_t len) { - m_bomType = DetectBOM(*src, *len); + m_bomType = DetectBOM(src, len); if ( m_bomType == BOM_Unknown ) return false; InitFromBOM(m_bomType); - SkipBOM(src, len); return true; } @@ -253,7 +252,7 @@ wxConvAuto::ToWChar(wchar_t *dst, size_t dstLen, if ( !m_conv ) { - if ( !self->InitFromInput(&src, &srcLen) ) + if ( !self->InitFromInput(src, srcLen) ) { // there is not enough data to determine whether we have a BOM or // not, so fail for now -- the caller is supposed to call us again @@ -261,9 +260,21 @@ wxConvAuto::ToWChar(wchar_t *dst, size_t dstLen, return wxCONV_FAILED; } } - else if ( !m_consumedBOM && dst ) + + if ( !m_consumedBOM ) { SkipBOM(&src, &srcLen); + if ( srcLen == 0 ) + { + // there is nothing left except the BOM so we'd return 0 below but + // this is unexpected: decoding a non-empty string must either fail + // or return something non-empty, in particular this would break + // the code in wxTextInputStream::NextChar() + // + // so still return an error as we need some more data to be able to + // decode it + return wxCONV_FAILED; + } } // try to convert using the auto-detected encoding @@ -286,8 +297,10 @@ wxConvAuto::ToWChar(wchar_t *dst, size_t dstLen, } } - if (rc != wxCONV_FAILED && dst && !m_consumedBOM) + // don't skip the BOM again the next time if we really consumed it + if ( rc != wxCONV_FAILED && dst && !m_consumedBOM ) self->m_consumedBOM = true; + return rc; }