re PR libstdc++/58839 (dereferencing void* in shared_ptr(unique_ptr&& u) constructor)

PR libstdc++/58839
	* include/bits/shared_ptr_base.h
	(__shared_ptr<T>::__shared_ptr(unique_ptr<U,D>&&)): Only use addressof
	when unique_ptr<U,D>::pointer is not a built-in pointer type.
	* testsuite/20_util/shared_ptr/cons/58839.cc: New.
	* testsuite/20_util/enable_shared_from_this/members/assign.cc: New.
	* testsuite/20_util/enable_shared_from_this/members/unique_ptr.cc: New.

From-SVN: r204184
This commit is contained in:
Jonathan Wakely 2013-10-29 21:33:29 +00:00 committed by Jonathan Wakely
parent da2e699194
commit c8c0f5577d
5 changed files with 149 additions and 2 deletions

View File

@ -1,3 +1,13 @@
2013-10-29 Jonathan Wakely <jwakely.gcc@gmail.com>
PR libstdc++/58839
* include/bits/shared_ptr_base.h
(__shared_ptr<T>::__shared_ptr(unique_ptr<U,D>&&)): Only use addressof
when unique_ptr<U,D>::pointer is not a built-in pointer type.
* testsuite/20_util/shared_ptr/cons/58839.cc: New.
* testsuite/20_util/enable_shared_from_this/members/assign.cc: New.
* testsuite/20_util/enable_shared_from_this/members/unique_ptr.cc: New.
2013-10-29 Jonathan Wakely <jwakely.gcc@gmail.com>
* include/bits/hashtable.cc (__access_protected_ctor): Define and use

View File

@ -855,9 +855,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: _M_ptr(__r.get()), _M_refcount()
{
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
auto __tmp = std::__addressof(*__r.get());
auto __raw = _S_raw_ptr(__r.get());
_M_refcount = __shared_count<_Lp>(std::move(__r));
__enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
__enable_shared_from_this_helper(_M_refcount, __raw, __raw);
}
#if _GLIBCXX_USE_DEPRECATED
@ -1048,6 +1048,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_M_get_deleter(const std::type_info& __ti) const noexcept
{ return _M_refcount._M_get_deleter(__ti); }
template<typename _Tp1>
static _Tp1*
_S_raw_ptr(_Tp1* __ptr)
{ return __ptr; }
template<typename _Tp1>
static auto
_S_raw_ptr(_Tp1 __ptr) -> decltype(std::__addressof(*__ptr))
{ return std::__addressof(*__ptr); }
template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;

View File

@ -0,0 +1,36 @@
// { dg-options "-std=gnu++11" }
// Copyright (C) 2013 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/>.
#include <memory>
#include <testsuite_hooks.h>
struct X : public std::enable_shared_from_this<X> { };
void test01()
{
bool test __attribute__((unused)) = true;
auto x1 = std::make_shared<X>(), x2 = std::make_shared<X>();
*x1 = *x2;
VERIFY( x1->shared_from_this() != x2->shared_from_this() );
}
int main()
{
test01();
}

View File

@ -0,0 +1,62 @@
// { dg-options "-std=gnu++11" }
// Copyright (C) 2013 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/>.
#include <memory>
#include <ext/pointer.h>
#include <testsuite_hooks.h>
struct X : public std::enable_shared_from_this<X> { };
void test01()
{
std::unique_ptr<X> up(new X);
X* xp = up.get();
std::shared_ptr<X> sp(std::move(up));
VERIFY( xp->shared_from_this() != nullptr );
}
using __gnu_cxx::_Pointer_adapter;
using __gnu_cxx::_Std_pointer_impl;
struct Deleter
{
struct pointer : _Pointer_adapter<_Std_pointer_impl<X>>
{
using _Pointer_adapter::_Pointer_adapter;
operator X*() const noexcept { return this->get(); }
};
void operator()(pointer p) const noexcept { delete (X*)p; }
};
void test02()
{
std::unique_ptr<X, Deleter> up(new X);
Deleter::pointer xp = up.get();
// Creating shared_ptr from unique_ptr with custom pointer is an extension:
std::shared_ptr<X> sp(std::move(up));
// but enable_shared_from_this should still work:
VERIFY( xp->shared_from_this() != nullptr );
}
int main()
{
test01();
test02();
}

View File

@ -0,0 +1,29 @@
// { dg-options "-std=gnu++11" }
// { dg-do compile }
// Copyright (C) 2013 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/>.
#include <memory>
// libstdc++/58839
void test01()
{
std::unique_ptr<void> y;
std::shared_ptr<void> x = std::move(y);
}