From: Vadim Zeitlin Date: Fri, 23 Mar 2012 15:13:08 +0000 (+0000) Subject: Fix caching wrong length in wxString(str, len) ctor in UTF-8 build. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/bd42a07c7a528c607e7327dcd194b739fb242da2 Fix caching wrong length in wxString(str, len) ctor in UTF-8 build. A length greater than that of the source string could be passed to this ctor. This worked correctly, i.e. created a string which was a copy of the source one but cached a wrong length for it. Avoid this by explicitly checking the length before caching it in wxString::assign(str, len). See #14130. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@70986 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/string.h b/include/wx/string.h index 8a38a4240b..4591e1b075 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -2613,9 +2613,21 @@ public: return *this; } + // This is a non-standard-compliant overload taking the first "len" + // characters of the source string. wxString& assign(const wxString& str, size_t len) { +#if wxUSE_STRING_POS_CACHE + // It is legal to pass len > str.length() to wxStringImpl::assign() but + // by restricting it here we save some work for that function so it's not + // really less efficient and, at the same time, ensure that we don't + // cache invalid length. + const size_t lenSrc = str.length(); + if ( len > lenSrc ) + len = lenSrc; + wxSTRING_SET_CACHED_LENGTH(len); +#endif // wxUSE_STRING_POS_CACHE m_impl.assign(str.m_impl, 0, str.LenToImpl(len)); diff --git a/tests/strings/strings.cpp b/tests/strings/strings.cpp index 43e3640520..879fab7907 100644 --- a/tests/strings/strings.cpp +++ b/tests/strings/strings.cpp @@ -190,6 +190,8 @@ void StringTestCase::Constructors() CPPUNIT_ASSERT_EQUAL( L"Hello", wxString(L"Hello", 5) ); #endif // wxUSE_UNICODE + CPPUNIT_ASSERT_EQUAL( 0, wxString(wxString(), 17).length() ); + static const char *s = "?really!"; const char *start = wxStrchr(s, 'r'); const char *end = wxStrchr(s, '!');