From e25d2617ac1e70229a108106430429c95334bd63 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fran=C3=A7ois=20Dumont?= Date: Wed, 24 Jun 2015 20:12:05 +0000 Subject: [PATCH] basic_string.h (basic_string<>::front()): Add !empty debug check. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 2015-06-24 François Dumont * include/bits/basic_string.h (basic_string<>::front()): Add !empty debug check. (basic_string<>::back()): Likewise. (basic_string<>::pop_back()): Likewise. From-SVN: r224919 --- libstdc++-v3/ChangeLog | 7 ++++ libstdc++-v3/include/bits/basic_string.h | 51 +++++++++++++++++++----- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 1880614ffff..47777ac7c49 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +2015-06-24 François Dumont + + * include/bits/basic_string.h (basic_string<>::front()): Add !empty + debug check. + (basic_string<>::back()): Likewise. + (basic_string<>::pop_back()): Likewise. + 2015-06-24 Paolo Carlini * testsuite/23_containers/array/tuple_interface/get_neg.cc: Adjust diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 093f5021de7..923fb83937c 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -39,6 +39,7 @@ #include #include #include + #if __cplusplus >= 201103L #include #endif @@ -903,7 +904,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ reference front() noexcept - { return operator[](0); } + { + _GLIBCXX_DEBUG_ASSERT(!empty()); + return operator[](0); + } /** * Returns a read-only (constant) reference to the data at the first @@ -911,7 +915,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ const_reference front() const noexcept - { return operator[](0); } + { + _GLIBCXX_DEBUG_ASSERT(!empty()); + return operator[](0); + } /** * Returns a read/write reference to the data at the last @@ -919,7 +926,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ reference back() noexcept - { return operator[](this->size() - 1); } + { + _GLIBCXX_DEBUG_ASSERT(!empty()); + return operator[](this->size() - 1); + } /** * Returns a read-only (constant) reference to the data at the @@ -927,7 +937,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ const_reference back() const noexcept - { return operator[](this->size() - 1); } + { + _GLIBCXX_DEBUG_ASSERT(!empty()); + return operator[](this->size() - 1); + } #endif // Modifiers: @@ -1506,7 +1519,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ void pop_back() noexcept - { _M_erase(size()-1, 1); } + { + _GLIBCXX_DEBUG_ASSERT(!empty()); + _M_erase(size() - 1, 1); + } #endif // C++11 /** @@ -3308,7 +3324,10 @@ _GLIBCXX_END_NAMESPACE_CXX11 */ reference front() - { return operator[](0); } + { + _GLIBCXX_DEBUG_ASSERT(!empty()); + return operator[](0); + } /** * Returns a read-only (constant) reference to the data at the first @@ -3316,7 +3335,10 @@ _GLIBCXX_END_NAMESPACE_CXX11 */ const_reference front() const _GLIBCXX_NOEXCEPT - { return operator[](0); } + { + _GLIBCXX_DEBUG_ASSERT(!empty()); + return operator[](0); + } /** * Returns a read/write reference to the data at the last @@ -3324,7 +3346,10 @@ _GLIBCXX_END_NAMESPACE_CXX11 */ reference back() - { return operator[](this->size() - 1); } + { + _GLIBCXX_DEBUG_ASSERT(!empty()); + return operator[](this->size() - 1); + } /** * Returns a read-only (constant) reference to the data at the @@ -3332,7 +3357,10 @@ _GLIBCXX_END_NAMESPACE_CXX11 */ const_reference back() const _GLIBCXX_NOEXCEPT - { return operator[](this->size() - 1); } + { + _GLIBCXX_DEBUG_ASSERT(!empty()); + return operator[](this->size() - 1); + } #endif // Modifiers: @@ -3819,7 +3847,10 @@ _GLIBCXX_END_NAMESPACE_CXX11 */ void pop_back() // FIXME C++11: should be noexcept. - { erase(size()-1, 1); } + { + _GLIBCXX_DEBUG_ASSERT(!empty()); + erase(size() - 1, 1); + } #endif // C++11 /** -- 2.30.2