diff options
author | Paul Pluzhnikov <ppluzhnikov@google.com> | 2013-01-22 03:30:17 +0000 |
---|---|---|
committer | Paul Pluzhnikov <ppluzhnikov@google.com> | 2013-01-22 03:30:17 +0000 |
commit | b85ec4fdbb22bb9e5f6642475fd01c734e217d51 (patch) | |
tree | c5b9d269af389902ab0a2f037b2602834cd16a0b | |
parent | a874f8445fcbdbead83561435da5d7607652b0f1 (diff) |
Add lightweight checks for front()/back() on empty vector.
Google ref b/7939186
git-svn-id: https://gcc.gnu.org/svn/gcc/branches/google/gcc-4_7@195356 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r-- | libstdc++-v3/include/bits/stl_vector.h | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index c5cf0498410..9addee87a8f 100644 --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -878,7 +878,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER */ reference front() - { return *begin(); } + { +#if __google_stl_debug_vector + if (empty()) __throw_logic_error("front() on empty vector"); +#endif + return *begin(); + } /** * Returns a read-only (constant) reference to the data at the first @@ -886,7 +891,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER */ const_reference front() const - { return *begin(); } + { +#if __google_stl_debug_vector + if (empty()) __throw_logic_error("front() on empty vector"); +#endif + return *begin(); + } /** * Returns a read/write reference to the data at the last @@ -894,7 +904,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER */ reference back() - { return *(end() - 1); } + { +#if __google_stl_debug_vector + if (empty()) __throw_logic_error("back() on empty vector"); +#endif + return *(end() - 1); + } /** * Returns a read-only (constant) reference to the data at the @@ -902,7 +917,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER */ const_reference back() const - { return *(end() - 1); } + { +#if __google_stl_debug_vector + if (empty()) __throw_logic_error("back() on empty vector"); +#endif + return *(end() - 1); + } // _GLIBCXX_RESOLVE_LIB_DEFECTS // DR 464. Suggestion for new member functions in standard containers. @@ -917,7 +937,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER pointer #endif data() _GLIBCXX_NOEXCEPT - { return std::__addressof(front()); } + { +#if __google_stl_debug_vector + if (empty()) return 0; +#endif + return std::__addressof(front()); + } #ifdef __GXX_EXPERIMENTAL_CXX0X__ const _Tp* @@ -925,7 +950,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER const_pointer #endif data() const _GLIBCXX_NOEXCEPT - { return std::__addressof(front()); } + { +#if __google_stl_debug_vector + if (empty()) return 0; +#endif + return std::__addressof(front()); + } // [23.2.4.3] modifiers /** |