Bug 179405 - Avoid using reinterpret_cast in WeakPtr downcasting
Summary: Avoid using reinterpret_cast in WeakPtr downcasting
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Template Framework (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-11-07 18:00 PST by Jiewen Tan
Modified: 2017-11-07 18:00 PST (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jiewen Tan 2017-11-07 18:00:20 PST
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& weakPtrFactory() { return m_weakPtrFactory; }

private:
    WeakPtrFactory<Base> 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 = &object;
    Base* basePtr = static_cast<Base*>(&object);
    EXPECT_NE((void*)basePtr, (void*)multiPtr);

    WeakPtr<Base> baseWeakPtr = object.weakPtrFactory().createWeakPtr(object);
    WeakPtr<Multi> 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