diff --git a/libstdc++-v3/include/debug/safe_iterator.h b/libstdc++-v3/include/debug/safe_iterator.h index f9068eaf8d6..e7c96d1af27 100644 --- a/libstdc++-v3/include/debug/safe_iterator.h +++ b/libstdc++-v3/include/debug/safe_iterator.h @@ -129,14 +129,6 @@ namespace __gnu_debug typename _Sequence::_Base::iterator, typename _Sequence::_Base::const_iterator>::__type _OtherIterator; - struct _Attach_single - { }; - - _Safe_iterator(_Iterator __i, _Safe_sequence_base* __seq, _Attach_single) - _GLIBCXX_NOEXCEPT - : _Iter_base(__i) - { _M_attach_single(__seq); } - public: typedef _Iterator iterator_type; typedef typename _Traits::iterator_category iterator_category; @@ -347,8 +339,13 @@ namespace __gnu_debug _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(), _M_message(__msg_bad_inc) ._M_iterator(*this, "this")); - __gnu_cxx::__scoped_lock __l(this->_M_get_mutex()); - return _Safe_iterator(base()++, this->_M_sequence, _Attach_single()); + _Iter_base __cur; + { + __gnu_cxx::__scoped_lock __l(this->_M_get_mutex()); + __cur = base()++; + } + + return _Safe_iterator(__cur, this->_M_sequence); } // ------ Utilities ------ @@ -520,12 +517,6 @@ namespace __gnu_debug protected: typedef typename _Safe_base::_OtherIterator _OtherIterator; - typedef typename _Safe_base::_Attach_single _Attach_single; - - _Safe_iterator(_Iterator __i, _Safe_sequence_base* __seq, _Attach_single) - _GLIBCXX_NOEXCEPT - : _Safe_base(__i, __seq, _Attach_single()) - { } public: /// @post the iterator is singular and unattached @@ -609,9 +600,13 @@ namespace __gnu_debug _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(), _M_message(__msg_bad_inc) ._M_iterator(*this, "this")); - __gnu_cxx::__scoped_lock __l(this->_M_get_mutex()); - return _Safe_iterator(this->base()++, this->_M_sequence, - _Attach_single()); + _Iter_base __cur; + { + __gnu_cxx::__scoped_lock __l(this->_M_get_mutex()); + __cur = this->base()++; + } + + return _Safe_iterator(__cur, this->_M_sequence); } // ------ Bidirectional iterator requirements ------ @@ -640,9 +635,13 @@ namespace __gnu_debug _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(), _M_message(__msg_bad_dec) ._M_iterator(*this, "this")); - __gnu_cxx::__scoped_lock __l(this->_M_get_mutex()); - return _Safe_iterator(this->base()--, this->_M_sequence, - _Attach_single()); + _Iter_base __cur; + { + __gnu_cxx::__scoped_lock __l(this->_M_get_mutex()); + __cur = this->base()--; + } + + return _Safe_iterator(__cur, this->_M_sequence); } // ------ Utilities ------ @@ -666,13 +665,6 @@ namespace __gnu_debug typedef _Safe_iterator<_OtherIterator, _Sequence, std::random_access_iterator_tag> _OtherSelf; - typedef typename _Safe_base::_Attach_single _Attach_single; - - _Safe_iterator(_Iterator __i, _Safe_sequence_base* __seq, _Attach_single) - _GLIBCXX_NOEXCEPT - : _Safe_base(__i, __seq, _Attach_single()) - { } - public: typedef typename _Safe_base::difference_type difference_type; typedef typename _Safe_base::reference reference; @@ -761,9 +753,16 @@ namespace __gnu_debug _Safe_iterator operator++(int) _GLIBCXX_NOEXCEPT { - _Safe_iterator __ret = *this; - ++*this; - return __ret; + _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(), + _M_message(__msg_bad_inc) + ._M_iterator(*this, "this")); + _Iter_base __cur; + { + __gnu_cxx::__scoped_lock __l(this->_M_get_mutex()); + __cur = this->base()++; + } + + return _Safe_iterator(__cur, this->_M_sequence); } // ------ Bidirectional iterator requirements ------ @@ -785,9 +784,16 @@ namespace __gnu_debug _Safe_iterator operator--(int) _GLIBCXX_NOEXCEPT { - _Safe_iterator __ret = *this; - --*this; - return __ret; + _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(), + _M_message(__msg_bad_dec) + ._M_iterator(*this, "this")); + _Iter_base __cur; + { + __gnu_cxx::__scoped_lock __l(this->_M_get_mutex()); + __cur = this->base()--; + } + + return _Safe_iterator(__cur, this->_M_sequence); } // ------ Random access iterator requirements ------ diff --git a/libstdc++-v3/include/debug/safe_local_iterator.h b/libstdc++-v3/include/debug/safe_local_iterator.h index 6e3c4eb1505..3c525652ea1 100644 --- a/libstdc++-v3/include/debug/safe_local_iterator.h +++ b/libstdc++-v3/include/debug/safe_local_iterator.h @@ -84,14 +84,6 @@ namespace __gnu_debug typedef _Safe_local_iterator _Self; typedef _Safe_local_iterator<_OtherIterator, _Sequence> _OtherSelf; - struct _Attach_single - { }; - - _Safe_local_iterator(_Iterator __i, _Safe_sequence_base* __cont, - _Attach_single) noexcept - : _Iter_base(__i) - { _M_attach_single(__cont); } - public: typedef _Iterator iterator_type; typedef typename _Traits::iterator_category iterator_category; @@ -290,9 +282,13 @@ namespace __gnu_debug _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(), _M_message(__msg_bad_inc) ._M_iterator(*this, "this")); - __gnu_cxx::__scoped_lock __l(this->_M_get_mutex()); - return _Safe_local_iterator(base()++, this->_M_sequence, - _Attach_single()); + _Iter_base __cur; + { + __gnu_cxx::__scoped_lock __l(this->_M_get_mutex()); + __cur = base()++; + } + + return { __cur, this->_M_sequence }; } // ------ Utilities ------ diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/debug/invalid_local_iterator_post_increment_neg.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/debug/invalid_local_iterator_post_increment_neg.cc new file mode 100644 index 00000000000..74005c3ec69 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/debug/invalid_local_iterator_post_increment_neg.cc @@ -0,0 +1,16 @@ +// { dg-do run { target c++11 xfail *-*-* } } +// { dg-require-debug-mode "" } + +#include +#include + +void test01() +{ + __gnu_test::invalid_local_iterator_post_increment>(); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/debug/invalid_local_iterator_pre_increment_neg.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/debug/invalid_local_iterator_pre_increment_neg.cc new file mode 100644 index 00000000000..016cd1c6947 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/debug/invalid_local_iterator_pre_increment_neg.cc @@ -0,0 +1,16 @@ +// { dg-do run { target c++11 xfail *-*-* } } +// { dg-require-debug-mode "" } + +#include +#include + +void test01() +{ + __gnu_test::invalid_local_iterator_pre_increment>(); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/util/debug/unordered_checks.h b/libstdc++-v3/testsuite/util/debug/unordered_checks.h index 655f16f199f..971fe68396b 100644 --- a/libstdc++-v3/testsuite/util/debug/unordered_checks.h +++ b/libstdc++-v3/testsuite/util/debug/unordered_checks.h @@ -125,6 +125,38 @@ namespace __gnu_test VERIFY( *it == val ); } + template + void invalid_local_iterator_pre_increment() + { + typedef _Tp cont_type; + typedef typename cont_type::value_type cont_val_type; + typedef typename CopyableValueType::value_type val_type; + generate_unique gu; + + cont_type c; + for (size_t i = 0; i != 5; ++i) + c.insert(gu.build()); + + for (auto lit = c.begin(0); ;) + ++lit; + } + + template + void invalid_local_iterator_post_increment() + { + typedef _Tp cont_type; + typedef typename cont_type::value_type cont_val_type; + typedef typename CopyableValueType::value_type val_type; + generate_unique gu; + + cont_type c; + for (size_t i = 0; i != 5; ++i) + c.insert(gu.build()); + + for (auto it = c.begin(0); ;) + it++; + } + template void invalid_local_iterator_compare() {