From fca038e13322f658147ccbb98638c54c3bc47061 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Fri, 8 Aug 2014 15:35:52 +0000 Subject: [PATCH] =?UTF-8?q?While=20reading=20LWG#526,=20Ion=20Gazta=C3=B1a?= =?UTF-8?q?ga=20noticed=20that=20libc++=20didn't=20correctly=20handle=20li?= =?UTF-8?q?st::remove(const=20value=5Ftype=20&x),=20if=20x=20was=20an=20el?= =?UTF-8?q?ement=20of=20the=20list.=20Added=20a=20test=20for=20this,=20and?= =?UTF-8?q?=20a=20fix.=20Thanks=20to=20Ion=20for=20the=20report.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@215210 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/list | 26 ++++++++------- .../sequences/list/list.ops/remove.pass.cpp | 32 +++++++++++++++++++ 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/include/list b/include/list index 1d21fd6f1..c36786dd4 100644 --- a/include/list +++ b/include/list @@ -219,7 +219,7 @@ struct __list_node_base _LIBCPP_INLINE_VISIBILITY pointer __self() { - return static_cast(pointer_traits<__base_pointer>::pointer_to(*this)); + return static_cast(pointer_traits<__base_pointer>::pointer_to(*this)); } }; @@ -1086,10 +1086,10 @@ inline _LIBCPP_INLINE_VISIBILITY void list<_Tp, _Alloc>::__link_nodes_at_front(__node_pointer __f, __node_pointer __l) { - __f->__prev_ = base::__end_.__self(); - __l->__next_ = base::__end_.__next_; - __l->__next_->__prev_ = __l; - base::__end_.__next_ = __f; + __f->__prev_ = base::__end_.__self(); + __l->__next_ = base::__end_.__next_; + __l->__next_->__prev_ = __l; + base::__end_.__next_ = __f; } // Link in nodes [__f, __l] at the front of the list @@ -1098,10 +1098,10 @@ inline _LIBCPP_INLINE_VISIBILITY void list<_Tp, _Alloc>::__link_nodes_at_back(__node_pointer __f, __node_pointer __l) { - __l->__next_ = base::__end_.__self(); - __f->__prev_ = base::__end_.__prev_; - __f->__prev_->__next_ = __f; - base::__end_.__prev_ = __l; + __l->__next_ = base::__end_.__self(); + __f->__prev_ = base::__end_.__prev_; + __f->__prev_->__next_ = __f; + base::__end_.__prev_ = __l; } @@ -2058,14 +2058,16 @@ template void list<_Tp, _Alloc>::remove(const value_type& __x) { - for (iterator __i = begin(), __e = end(); __i != __e;) + list<_Tp, _Alloc> __deleted_nodes; // collect the nodes we're removing + for (const_iterator __i = begin(), __e = end(); __i != __e;) { if (*__i == __x) { - iterator __j = _VSTD::next(__i); + const_iterator __j = _VSTD::next(__i); for (; __j != __e && *__j == __x; ++__j) ; - __i = erase(__i, __j); + __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j); + __i = __j; if (__i != __e) ++__i; } diff --git a/test/containers/sequences/list/list.ops/remove.pass.cpp b/test/containers/sequences/list/list.ops/remove.pass.cpp index 76d878d02..106c0527f 100644 --- a/test/containers/sequences/list/list.ops/remove.pass.cpp +++ b/test/containers/sequences/list/list.ops/remove.pass.cpp @@ -16,6 +16,17 @@ #include "min_allocator.h" +struct S { + S(int i) : i_(new int(i)) {} + S(const S &rhs) : i_(new int(*rhs.i_)) {} + S& operator = (const S &rhs) { *i_ = *rhs.i_; return *this; } + ~S () { delete i_; i_ = NULL; } + bool operator == (const S &rhs) const { return *i_ == *rhs.i_; } + int get () const { return *i_; } + int *i_; + }; + + int main() { { @@ -25,6 +36,27 @@ int main() c.remove(3); assert(c == std::list(a2, a2+3)); } + { // LWG issue #526 + int a1[] = {1, 2, 1, 3, 5, 8, 11}; + int a2[] = { 2, 3, 5, 8, 11}; + std::list c(a1, a1+7); + c.remove(c.front()); + assert(c == std::list(a2, a2+5)); + } + { + int a1[] = {1, 2, 1, 3, 5, 8, 11, 1}; + int a2[] = { 2, 3, 5, 8, 11 }; + std::list c; + for(int *ip = a1; ip < a1+8; ++ip) + c.push_back(S(*ip)); + c.remove(c.front()); + std::list::const_iterator it = c.begin(); + for(int *ip = a2; ip < a2+5; ++ip, ++it) { + assert ( it != c.end()); + assert ( *ip == it->get()); + } + assert ( it == c.end ()); + } #if __cplusplus >= 201103L { int a1[] = {1, 2, 3, 4};