diff --git a/include/wx/dynarray.h b/include/wx/dynarray.h index afd25b7e68..611655e602 100644 --- a/include/wx/dynarray.h +++ b/include/wx/dynarray.h @@ -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() ); diff --git a/tests/arrays/arrays.cpp b/tests/arrays/arrays.cpp index 5875c4f1ad..3ada06fb64 100644 --- a/tests/arrays/arrays.cpp +++ b/tests/arrays/arrays.cpp @@ -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);