<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - weak_ptr(weak_ptr const &) can segfault in the presence of virtual bases"
   href="https://bugs.llvm.org/show_bug.cgi?id=41114">41114</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>weak_ptr(weak_ptr const &) can segfault in the presence of virtual bases
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>arthur.j.odwyer@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>cat >test.cc <<EOF
#include <memory>
#include <stdio.h>

struct A { int i = 0; virtual ~A() {} };
struct B : public virtual A { char d[100000]; };

int main() {
    std::weak_ptr<B> p = std::shared_ptr<B>(new B);
    puts("hello world");
    std::weak_ptr<A> q = p;
}
EOF
clang++ -std=c++11 test.cc
./a.out

Segmentation fault


What's going on here is that the converting constructor weak_ptr(const
weak_ptr&) is implemented as

weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
                        typename enable_if<is_convertible<_Yp*, _Tp*>::value,
__nat*>::type)
                         _NOEXCEPT
    : __ptr_(__r.__ptr_),     // LINE XXX
      __cntrl_(__r.__cntrl_)
{
    if (__cntrl_)
        __cntrl_->__add_weak();
}

When `_Tp` is a virtual base of `_Yp`, the conversion on line XXX generally
needs to dereference `__r.__ptr_` to compute the appropriate offset. This is
fine if the weak_ptr stores a null pointer value, and it's fine if the weak_ptr
is not-expired; but if the weak_ptr is expired (or empty) and stores a non-null
pointer value, then we dereference into freed memory and segfault.

The solution adopted by both libstdc++ and MSVC is to implement this
constructor as essentially

weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
                        typename enable_if<is_convertible<_Yp*, _Tp*>::value,
__nat*>::type)
                         _NOEXCEPT
    : weak_ptr(__r.lock())
{}</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>