Fix libstdc++/55320

Jonathan Wakely jwakely.gcc@gmail.com
Wed Nov 14 23:30:00 GMT 2012


        PR libstdc++/55320
        * include/std/functional (function::function(F)): Set _M_manager after
        operations that could throw.
        (_Function_base::_Ref_manager::_M_init_functor): Use addressof.
        * include/tr1/functional
        (_Function_base::_Ref_manager::_M_init_functor): Use addressof.
        (_Function_base::_Base_manager::_M_get_pointer): Likewise.
        * testsuite/20_util/function/cons/55320.cc: New.
        * testsuite/20_util/function/cons/addressof.cc: New.
        * testsuite/20_util/function/cons/callable.cc: Remove header.
        * testsuite/20_util/bind/ref_neg.cc: Adjust dg-error line numbers.
        * testsuite/tr1/3_function_objects/function/10.cc: New.

Tested x86-64_linux, committed to trunk, 4.7 to follow.
-------------- next part --------------
commit d002bbfd0a4d46da09e13e9700e329dccc14d3d5
Author: Jonathan Wakely <jwakely.gcc@gmail.com>
Date:   Wed Nov 14 22:46:45 2012 +0000

    	PR libstdc++/55320
    	* include/std/functional (function::function(F)): Set _M_manager after
    	operations that could throw.
    	(_Function_base::_Ref_manager::_M_init_functor): Use addressof.
    	* include/tr1/functional
    	(_Function_base::_Ref_manager::_M_init_functor): Use addressof.
    	(_Function_base::_Base_manager::_M_get_pointer): Likewise.
    	* testsuite/20_util/function/cons/55320.cc: New.
    	* testsuite/20_util/function/cons/addressof.cc: New.
    	* testsuite/20_util/function/cons/callable.cc: Remove header.
    	* testsuite/20_util/bind/ref_neg.cc: Adjust dg-error line numbers.
    	* testsuite/tr1/3_function_objects/function/10.cc: New.

diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional
index 4dbf4d5..561359c 100644
--- a/libstdc++-v3/include/std/functional
+++ b/libstdc++-v3/include/std/functional
@@ -1,7 +1,6 @@
 // <functional> -*- C++ -*-
 
-// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
-// 2011, 2012 Free Software Foundation, Inc.
+// Copyright (C) 2001-2012 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
@@ -1882,8 +1881,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
 	static void
 	_M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
 	{
-	  // TBD: Use address_of function instead.
-	  _Base::_M_init_functor(__functor, &__f.get());
+	  _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
 	}
       };
 
@@ -2317,9 +2315,9 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
 
 	if (_My_handler::_M_not_empty_function(__f))
 	  {
+	    _My_handler::_M_init_functor(_M_functor, std::move(__f));
 	    _M_invoker = &_My_handler::_M_invoke;
 	    _M_manager = &_My_handler::_M_manager;
-	    _My_handler::_M_init_functor(_M_functor, std::move(__f));
 	  }
       }
 
diff --git a/libstdc++-v3/include/tr1/functional b/libstdc++-v3/include/tr1/functional
index 88a8149..61d6455 100644
--- a/libstdc++-v3/include/tr1/functional
+++ b/libstdc++-v3/include/tr1/functional
@@ -1615,7 +1615,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	_M_get_pointer(const _Any_data& __source)
 	{
 	  const _Functor* __ptr =
-	    __stored_locally? &__source._M_access<_Functor>()
+	    __stored_locally? std::__addressof(__source._M_access<_Functor>())
 	    /* have stored a pointer */ : __source._M_access<_Functor*>();
 	  return const_cast<_Functor*>(__ptr);
 	}
@@ -1744,8 +1744,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	static void
 	_M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
 	{
-	  // TBD: Use address_of function instead.
-	  _Base::_M_init_functor(__functor, &__f.get());
+	  _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
 	}
       };
 
diff --git a/libstdc++-v3/testsuite/20_util/bind/ref_neg.cc b/libstdc++-v3/testsuite/20_util/bind/ref_neg.cc
index 4534d1d..f5b5a01 100644
--- a/libstdc++-v3/testsuite/20_util/bind/ref_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/bind/ref_neg.cc
@@ -1,4 +1,4 @@
-// Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+// Copyright (C) 2010-2012 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
@@ -15,7 +15,7 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// 20.7.11 Function template bind
+// 20.8.9 Function template bind
 
 // { dg-do compile }
 // { dg-options "-std=gnu++0x" }
@@ -30,10 +30,10 @@ void test01()
 {
   const int dummy = 0;
   std::bind(&inc, _1)(0);               // { dg-error  "no match" }
-  // { dg-error "rvalue|const" "" { target *-*-* } 1207 }
-  // { dg-error "rvalue|const" "" { target *-*-* } 1221 }
-  // { dg-error "rvalue|const" "" { target *-*-* } 1235 }
-  // { dg-error "rvalue|const" "" { target *-*-* } 1249 }
+  // { dg-error "rvalue|const" "" { target *-*-* } 1206 }
+  // { dg-error "rvalue|const" "" { target *-*-* } 1220 }
+  // { dg-error "rvalue|const" "" { target *-*-* } 1234 }
+  // { dg-error "rvalue|const" "" { target *-*-* } 1248 }
   std::bind(&inc, std::ref(dummy))();	// { dg-error  "no match" }
 }
 
diff --git a/libstdc++-v3/testsuite/20_util/function/cons/55320.cc b/libstdc++-v3/testsuite/20_util/function/cons/55320.cc
new file mode 100644
index 0000000..611e8ac
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/function/cons/55320.cc
@@ -0,0 +1,46 @@
+// Copyright (C) 2012 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/>.
+
+// libstdc++/55320
+
+// { dg-options "-std=gnu++0x" }
+
+#include <functional>
+#include <testsuite_hooks.h>
+
+struct X
+{
+  X() { ++count; }
+  X(const X&) { throw 1; }
+  ~X() { --count; }
+  void operator()() { }
+  static int count;
+};
+
+int X::count = 0;
+
+int main()
+{
+  try
+  {
+    std::function<void()> f = X();
+  }
+  catch (int)
+  {
+    VERIFY( X::count == 0 );
+  }
+}
diff --git a/libstdc++-v3/testsuite/20_util/function/cons/addressof.cc b/libstdc++-v3/testsuite/20_util/function/cons/addressof.cc
new file mode 100644
index 0000000..25b6fb8
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/function/cons/addressof.cc
@@ -0,0 +1,41 @@
+// Copyright (C) 2012 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++0x" }
+// { dg-do compile }
+
+#include <functional>
+
+struct F
+{
+  void operator()() { }
+  void operator&() const { }
+};
+
+void test01()
+{
+  F f;
+  std::function<void()> f1 = f;
+  std::function<void()> f2 = std::ref(f);
+}
+
+int main()
+{
+  test01();
+
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/20_util/function/cons/callable.cc b/libstdc++-v3/testsuite/20_util/function/cons/callable.cc
index 209c404..9f16125 100644
--- a/libstdc++-v3/testsuite/20_util/function/cons/callable.cc
+++ b/libstdc++-v3/testsuite/20_util/function/cons/callable.cc
@@ -19,7 +19,6 @@
 // <http://www.gnu.org/licenses/>.
 
 #include <functional>
-#include <testsuite_hooks.h>
 
 void* f(std::function<void()>) { return nullptr; }
 int f(std::function<void(int)>) { return 1; }
diff --git a/libstdc++-v3/testsuite/tr1/3_function_objects/function/10.cc b/libstdc++-v3/testsuite/tr1/3_function_objects/function/10.cc
new file mode 100644
index 0000000..ca29e3c
--- /dev/null
+++ b/libstdc++-v3/testsuite/tr1/3_function_objects/function/10.cc
@@ -0,0 +1,41 @@
+// Copyright (C) 2012 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++0x" }
+// { dg-do compile }
+
+#include <tr1/functional>
+
+struct F
+{
+  void operator()() { }
+  void operator&() const { }
+};
+
+void test01()
+{
+  F f;
+  std::tr1::function<void()> f1 = f;
+  std::tr1::function<void()> f2 = std::tr1::ref(f);
+}
+
+int main()
+{
+  test01();
+
+  return 0;
+}


More information about the Gcc-patches mailing list