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
This commit is contained in:
Vadim Zeitlin 2012-03-23 15:13:08 +00:00
parent d1485d8177
commit bd42a07c7a
2 changed files with 14 additions and 0 deletions

View File

@ -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));

View File

@ -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, '!');