mirror of
https://github.com/wxWidgets/wxWidgets.git
synced 2026-09-22 22:24:56 +08:00
Assert that index is valid in wxArray::operator[]
Restore compatibility with all the previous wxWidgets versions that had this assert and make sure that errors in the code using this operator don't go unnoticed, as it happened with #26148. Closes #26149.
This commit is contained in:
@@ -122,6 +122,18 @@ public:
|
||||
bool IsEmpty() const { return this->empty(); }
|
||||
size_t Count() const { return this->size(); }
|
||||
|
||||
T& operator[](size_t uiIndex)
|
||||
{
|
||||
wxASSERT( uiIndex < this->size() );
|
||||
return base_vec::operator[](uiIndex);
|
||||
}
|
||||
|
||||
const T& operator[](size_t uiIndex) const
|
||||
{
|
||||
wxASSERT( uiIndex < this->size() );
|
||||
return base_vec::operator[](uiIndex);
|
||||
}
|
||||
|
||||
T& Item(size_t uiIndex) const
|
||||
{
|
||||
wxASSERT( uiIndex < this->size() );
|
||||
|
||||
@@ -321,6 +321,11 @@ TEST_CASE("wxArrayString", "[dynarray]")
|
||||
CHECK( a5.size() == 3 );
|
||||
CHECK( a5[2] == "Foo" );
|
||||
|
||||
// This is undefined behaviour but because the array has just been resized
|
||||
// down, its memory hopefully hasn't been reallocated yet, so we shouldn't
|
||||
// crash accessing it. But we must assert due to the index being invalid.
|
||||
WX_ASSERT_FAILS_WITH_ASSERT( a5[3].clear() );
|
||||
|
||||
wxArrayString a6;
|
||||
a6.Add("Foo");
|
||||
a6.Insert(a6[0], 1, 100);
|
||||
|
||||
Reference in New Issue
Block a user