Check for self-assignment in wxArrayString

Assigning array to itself destroyed its contents -- check for this now.

Closes #17619.
This commit is contained in:
Vadim Zeitlin 2016-08-09 01:07:59 +02:00
parent 3572c2c654
commit 985ff1e26e
2 changed files with 14 additions and 0 deletions

View File

@ -89,7 +89,14 @@ wxArrayString::wxArrayString(const wxArrayString& src)
wxArrayString& wxArrayString::operator=(const wxArrayString& src)
{
if ( m_nSize > 0 )
{
// Do this test here to avoid unnecessary overhead when assigning to an
// empty array, in that case there is no harm in self-assignment.
if ( &src == this )
return *this;
Clear();
}
Copy(src);

View File

@ -350,6 +350,13 @@ void ArraysTestCase::wxStringArrayTest()
wxArrayString a6;
a6.Add("Foo");
a6.Insert(a6[0], 1, 100);
wxArrayString a7;
a7 = a7;
CPPUNIT_ASSERT_EQUAL( 0, a7.size() );
a7.Add("Bar");
a7 = a7;
CPPUNIT_ASSERT_EQUAL( 1, a7.size() );
}
void ArraysTestCase::SortedArray()