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:
Vadim Zeitlin
2026-02-02 23:42:11 +01:00
parent 9d2a24b9f3
commit 01b5b06874
2 changed files with 17 additions and 0 deletions
+12
View File
@@ -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() );
+5
View File
@@ -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);