re PR libstdc++/55320 (Invalid delete with throwing copy-c'tor passed to std::function)

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.

From-SVN: r193514
This commit is contained in:
Jonathan Wakely 2012-11-14 23:33:01 +00:00 committed by Jonathan Wakely
parent 3aac5aad16
commit 99ace34a31
8 changed files with 154 additions and 15 deletions

View File

@ -1,3 +1,18 @@
2012-11-14 Jonathan Wakely <jwakely.gcc@gmail.com>
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.
2012-11-14 Uros Bizjak <ubizjak@gmail.com>
* testsuite/26_numerics/complex/abi_tag.cc: Adjust expected

View File

@ -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));
}
}

View File

@ -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()));
}
};

View File

@ -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" }
}

View File

@ -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 );
}
}

View File

@ -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;
}

View File

@ -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; }

View File

@ -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;
}