<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>179405</bug_id>
          
          <creation_ts>2017-11-07 18:00:20 -0800</creation_ts>
          <short_desc>Avoid using reinterpret_cast in WeakPtr downcasting</short_desc>
          <delta_ts>2017-11-07 18:00:56 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Web Template Framework</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Jiewen Tan">jiewen_tan</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>cdumez</cc>
    
    <cc>ggaren</cc>
    
    <cc>jiewen_tan</cc>
    
    <cc>koivisto</cc>
    
    <cc>rniwa</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1369294</commentid>
    <comment_count>0</comment_count>
    <who name="Jiewen Tan">jiewen_tan</who>
    <bug_when>2017-11-07 18:00:20 -0800</bug_when>
    <thetext>We should probably avoid using reinterpret_cast in WeakPtr downcasting as it might point to a wrong pointer if multiple inheritance is involved.

Examples:
class Base {
public:
    virtual ~Base()
    {
    }

    int foo()
    {
        return 0;
    }

    auto&amp; weakPtrFactory() { return m_weakPtrFactory; }

private:
    WeakPtrFactory&lt;Base&gt; m_weakPtrFactory;
};

class ExtraBase {
public:
    virtual ~ExtraBase() { }
    int someExtraData { 1 };
};

class Multi : public ExtraBase, public Base {
public:
    int foo()
    {
        return 1;
    }
};

TEST(WTF_WeakPtr, MultiInheritance)
{
    Multi object;
    Multi* multiPtr = &amp;object;
    Base* basePtr = static_cast&lt;Base*&gt;(&amp;object);
    EXPECT_NE((void*)basePtr, (void*)multiPtr);

    WeakPtr&lt;Base&gt; baseWeakPtr = object.weakPtrFactory().createWeakPtr(object);
    WeakPtr&lt;Multi&gt; multiWeakPtr = makeWeakPtr(object);
    EXPECT_NE((void*)baseWeakPtr.get(), (void*)multiWeakPtr.get());
}

One can copy and paste the above example into API test of WeakPtr and run the test. The result is:
jwtan$ run-api-tests WTF_WeakPtr.MultiInheritance
Running build-api-tests
FAIL WTF_WeakPtr.MultiInheritance

/Users/jwtan/Documents/Source/OpenSource/Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp:280
Expected: ((void*)baseWeakPtr.get()) != ((void*)multiWeakPtr.get()), actual: 0x7ffee36e7218 vs 0x7ffee36e7218


Tests that failed:
  WTF_WeakPtr.MultiInheritance</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>