From 5495389db59a07b2740c4bd339909b6ad95c8d8d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 11 Jul 2020 19:00:38 +0200 Subject: [PATCH] Fix off-by-one bug in wxVector::reverse_iterator::base() This is embarrassing, but the iterator returned by this method seems to have always been wrong, ever since it was added back in 946954d3bf (Added reverse iterator to wxVector, 2008-09-16). Moreover, it was also broken in its const_reverse_iterator counterpart where it was copy-and-pasted in f7ef20685f (Add wxVector<>::const_reverse_iterator, 2013-05-08). Finally fix this to return the correct value and add at least a simple unit test check that this method behaves as expected. --- include/wx/vector.h | 4 ++-- tests/vectors/vectors.cpp | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/wx/vector.h b/include/wx/vector.h index e05920f985..771b25bc0a 100644 --- a/include/wx/vector.h +++ b/include/wx/vector.h @@ -198,7 +198,7 @@ public: reference operator*() const { return *m_ptr; } pointer operator->() const { return m_ptr; } - iterator base() const { return m_ptr; } + iterator base() const { return m_ptr + 1; } reverse_iterator& operator++() { --m_ptr; return *this; } @@ -261,7 +261,7 @@ public: const_reference operator*() const { return *m_ptr; } const_pointer operator->() const { return m_ptr; } - const_iterator base() const { return m_ptr; } + const_iterator base() const { return m_ptr + 1; } const_reverse_iterator& operator++() { --m_ptr; return *this; } diff --git a/tests/vectors/vectors.cpp b/tests/vectors/vectors.cpp index 5c393afbb8..056b9303e6 100644 --- a/tests/vectors/vectors.cpp +++ b/tests/vectors/vectors.cpp @@ -334,6 +334,10 @@ TEST_CASE("wxVector::reverse_iterator", "[vector][reverse_iterator]") CHECK( ri < re ); CHECK( ri <= re ); + CHECK( rb.base() == v.end() ); + CHECK( re.base() == v.begin() ); + CHECK( *ri.base() == 9 ); + #if wxUSE_STD_CONTAINERS_COMPATIBLY std::vector stdvec(rb, re); REQUIRE( stdvec.size() == 10 );