//===----------------------------------------------------------------------===// // // 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 // //===----------------------------------------------------------------------===// // // weak_ptr // template weak_ptr(const weak_ptr& r); // template weak_ptr(weak_ptr&& 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 #include #include #include #include #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::value>::type buffer; #endif B* pb = ::new ((void*)&buffer) B(); std::shared_ptr sp = std::shared_ptr(pb, Deleter()); std::weak_ptr wp = sp; sp = nullptr; assert(wp.expired()); // Overwrite the B object with junk. std::memset(&buffer, '*', sizeof(buffer)); std::weak_ptr wq = wp; assert(wq.expired()); std::weak_ptr wr = std::move(wp); assert(wr.expired()); return 0; }