diff mbox

Make std::forward_list safe against overloaded operator&

Message ID 20150617113335.GI13009@redhat.com
State New
Headers show

Commit Message

Jonathan Wakely June 17, 2015, 11:33 a.m. UTC
Similar to yesterday's patch for std::list.

Also removes some invalid downcasts of the _Node_base sentinel's
address to _Node*. The casts aren't needed anyway, as we only access
members of the base class through those pointers.

Also removes some redundant upcasts using *static_cast<Base*>(&x)
which will fail if there's an operator& overload associated with Base.
We can just rely on the implicit conversion to Base& instead.

Tested powerpc64le-linux, committed to trunk.
diff mbox

Patch

commit 0cba111f883032046a6e951a32bb3fddc2e64efd
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Jun 17 12:00:45 2015 +0100

    	* include/bits/forward_list.h (forward_list::_M_get_Node_allocator):
    	Remove unnecessary uses of operator& and static_cast.
    	* include/bits/forward_list.tcc
    	(forward_list::operator=(const forward_list&)): Use __addressof
    	instead of operator&.
    	(forward_list::remove(const _Tp&), forward_list::remove(_Pred)):
    	Remove invalid static_casts.
    	* include/debug/forward_list: Use __addressof instead of operator&.
    	* testsuite/23_containers/forward_list/modifiers/addressof.cc: New.

diff --git a/libstdc++-v3/include/bits/forward_list.h b/libstdc++-v3/include/bits/forward_list.h
index dcb696f..d611ade 100644
--- a/libstdc++-v3/include/bits/forward_list.h
+++ b/libstdc++-v3/include/bits/forward_list.h
@@ -305,11 +305,11 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 
       _Node_alloc_type&
       _M_get_Node_allocator() noexcept
-      { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
+      { return this->_M_impl; }
 
       const _Node_alloc_type&
       _M_get_Node_allocator() const noexcept
-      { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
+      { return this->_M_impl; }
 
       _Fwd_list_base()
       : _M_impl() { }
diff --git a/libstdc++-v3/include/bits/forward_list.tcc b/libstdc++-v3/include/bits/forward_list.tcc
index 8c85cdc..1e1a02e 100644
--- a/libstdc++-v3/include/bits/forward_list.tcc
+++ b/libstdc++-v3/include/bits/forward_list.tcc
@@ -155,7 +155,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     forward_list<_Tp, _Alloc>::
     operator=(const forward_list& __list)
     {
-      if (&__list != this)
+      if (std::__addressof(__list) != this)
         {
 	  if (_Node_alloc_traits::_S_propagate_on_copy_assign())
 	    {
@@ -299,8 +299,8 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     forward_list<_Tp, _Alloc>::
     remove(const _Tp& __val)
     {
-      _Node* __curr = static_cast<_Node*>(&this->_M_impl._M_head);
-      _Node* __extra = 0;
+      _Node_base* __curr = &this->_M_impl._M_head;
+      _Node_base* __extra = nullptr;
 
       while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next))
         {
@@ -314,7 +314,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	      else
 		__extra = __curr;
 	    }
-	  __curr = static_cast<_Node*>(__curr->_M_next);
+	  __curr = __curr->_M_next;
         }
 
       if (__extra)
@@ -327,13 +327,13 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       forward_list<_Tp, _Alloc>::
       remove_if(_Pred __pred)
       {
-	_Node* __curr = static_cast<_Node*>(&this->_M_impl._M_head);
+	_Node_base* __curr = &this->_M_impl._M_head;
         while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next))
           {
             if (__pred(*__tmp->_M_valptr()))
               this->_M_erase_after(__curr);
             else
-              __curr = static_cast<_Node*>(__curr->_M_next);
+              __curr = __curr->_M_next;
           }
       }
 
diff --git a/libstdc++-v3/include/debug/forward_list b/libstdc++-v3/include/debug/forward_list
index 0f09d23..d22c22f 100644
--- a/libstdc++-v3/include/debug/forward_list
+++ b/libstdc++-v3/include/debug/forward_list
@@ -105,7 +105,7 @@  namespace __gnu_debug
 	      __bbegin_its = __victim_base;
 	    }
 	  else
-	    __victim_base->_M_sequence = &__lhs;
+	    __victim_base->_M_sequence = std::__addressof(__lhs);
 	}
 
       if (__bbegin_its)
@@ -523,7 +523,7 @@  namespace __debug
       splice_after(const_iterator __pos, forward_list&& __list)
       {
 	__glibcxx_check_insert_after(__pos);
-	_GLIBCXX_DEBUG_VERIFY(&__list != this,
+	_GLIBCXX_DEBUG_VERIFY(std::__addressof(__list) != this,
 			      _M_message(__gnu_debug::__msg_self_splice)
 			      ._M_sequence(*this, "this"));
 	_GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(),
@@ -550,7 +550,7 @@  namespace __debug
 	_GLIBCXX_DEBUG_VERIFY(__i._M_before_dereferenceable(),
 			      _M_message(__gnu_debug::__msg_splice_bad)
 			      ._M_iterator(__i, "__i"));
-	_GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(&__list),
+	_GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(std::__addressof(__list)),
 			      _M_message(__gnu_debug::__msg_splice_other)
 			      ._M_iterator(__i, "__i")
 			      ._M_sequence(__list, "__list"));
@@ -577,9 +577,10 @@  namespace __debug
       splice_after(const_iterator __pos, forward_list&& __list,
 		   const_iterator __before, const_iterator __last)
       {
+	auto __listptr = std::__addressof(__list);
 	__glibcxx_check_insert_after(__pos);
 	__glibcxx_check_valid_range(__before, __last);
-	_GLIBCXX_DEBUG_VERIFY(__before._M_attached_to(&__list),
+	_GLIBCXX_DEBUG_VERIFY(__before._M_attached_to(__listptr),
 			      _M_message(__gnu_debug::__msg_splice_other)
 			      ._M_sequence(__list, "list")
 			      ._M_iterator(__before, "before"));
@@ -607,7 +608,7 @@  namespace __debug
 				  ._M_sequence(__list, "list")
 				  ._M_iterator(__before, "before")
 				  ._M_iterator(__last, "last"));
-	    _GLIBCXX_DEBUG_VERIFY(&__list != this || __tmp != __pos.base(),
+	    _GLIBCXX_DEBUG_VERIFY(__listptr != this || __tmp != __pos.base(),
 				  _M_message(__gnu_debug::__msg_splice_overlap)
 				  ._M_iterator(__tmp, "position")
 				  ._M_iterator(__before, "before")
@@ -694,7 +695,7 @@  namespace __debug
       void
       merge(forward_list&& __list)
       {
-	if (this != &__list)
+	if (this != std::__addressof(__list))
 	{
 	  __glibcxx_check_sorted(_Base::begin(), _Base::end());
 	  __glibcxx_check_sorted(__list._M_base().begin(),
@@ -716,7 +717,7 @@  namespace __debug
 	void
 	merge(forward_list&& __list, _Comp __comp)
 	{
-	  if (this != &__list)
+	  if (this != std::__addressof(__list))
 	  {
 	    __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(), __comp);
 	    __glibcxx_check_sorted_pred(__list._M_base().begin(),
diff --git a/libstdc++-v3/testsuite/23_containers/forward_list/modifiers/addressof.cc b/libstdc++-v3/testsuite/23_containers/forward_list/modifiers/addressof.cc
new file mode 100644
index 0000000..3c325f7
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/forward_list/modifiers/addressof.cc
@@ -0,0 +1,32 @@ 
+// Copyright (C) 2015 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++11" }
+// { dg-do compile }
+
+#include <forward_list>
+
+namespace N
+{
+  // This operator& must not be found by ADL.
+  template<typename T> void operator&(const T&) { }
+  struct X { };
+  bool operator==(const X&, const X&);
+  bool operator<(const X&, const X&);
+}
+
+template class std::forward_list<N::X>;