Skip to content

[libc++] Fix a segfault in weak_ptr(const weak_ptr<Y>&) #67956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions libcxx/include/__memory/shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1727,11 +1727,11 @@ template<class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
inline
weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r)
_NOEXCEPT
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
: __ptr_(nullptr),
__cntrl_(nullptr)
{
if (__cntrl_)
__cntrl_->__add_weak();
shared_ptr<_Yp> __s = __r.lock();
*this = weak_ptr<_Tp>(__s);
}

template<class _Tp>
Expand All @@ -1749,11 +1749,12 @@ template<class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
inline
weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r)
_NOEXCEPT
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
: __ptr_(nullptr),
__cntrl_(nullptr)
{
__r.__ptr_ = nullptr;
__r.__cntrl_ = nullptr;
shared_ptr<_Yp> __s = __r.lock();
*this = weak_ptr<_Tp>(__s);
__r.reset();
}

template<class _Tp>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// <memory>

// weak_ptr

// template<class Y> weak_ptr(const weak_ptr<Y>& r);
// template<class Y> weak_ptr(weak_ptr<Y>&& r);
//
// Regression test for https://github.com/llvm/llvm-project/issues/40459
// Verify that these constructors never attempt a derived-to-virtual-base
// conversion on a dangling weak_ptr.

#include <cassert>
#include <cstring>
#include <memory>
#include <utility>

#include "test_macros.h"

struct A {
int i;
virtual ~A() {}
};
struct B : public virtual A {
int j;
};
struct Deleter {
void operator()(void*) const {
// do nothing
}
};

int main(int, char**) {
#if TEST_STD_VER >= 11
alignas(B) char buffer[sizeof(B)];
#else
std::aligned_storage<sizeof(B), std::alignment_of<B>::value>::type buffer;
#endif
B* pb = ::new ((void*)&buffer) B();
std::shared_ptr<B> sp = std::shared_ptr<B>(pb, Deleter());
std::weak_ptr<B> wp = sp;
sp = nullptr;
assert(wp.expired());

// Overwrite the B object with junk.
std::memset(&buffer, '*', sizeof(buffer));

std::weak_ptr<A> wq = wp;
assert(wq.expired());
std::weak_ptr<A> wr = std::move(wp);
assert(wr.expired());

return 0;
}