Skip to content

Commit d84ad81

Browse files
committed
[WTF] Strengthen Box when it's build bare
https://bugs.webkit.org/show_bug.cgi?id=296689 Reviewed by Chris Dumez. When building an empty Box you can end up with an empty RefPtr. We should ensure it's valid before accessing it. Test: Tools/TestWebKitAPI/Tests/WTF/BoxPtr.cpp * Source/WTF/wtf/Box.h: (WTF::Box::isValid const): (WTF::Box::get const): (WTF::Box::operator* const): (WTF::Box::operator-> const): (WTF::Box::operator bool const): * Tools/TestWebKitAPI/Tests/WTF/BoxPtr.cpp: (TestWebKitAPI::BoxPtrClassTest::method): (TestWebKitAPI::boxPtrClassTestDeleter): (TestWebKitAPI::TEST(WTF_BoxPtr, NoCrash)): Canonical link: https://commits.webkit.org/300573@main
1 parent fa6096c commit d84ad81

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

Source/WTF/wtf/Box.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,28 @@ class Box {
5555
return result;
5656
}
5757

58-
T* get() const { return &m_data->value; }
58+
bool isValid() const { return static_cast<bool>(m_data); }
5959

60-
T& operator*() const { return m_data->value; }
61-
T* operator->() const { return &m_data->value; }
60+
T* get() const
61+
{
62+
if (!isValid())
63+
return nullptr;
64+
return &m_data->value;
65+
}
66+
67+
T& operator*() const { RELEASE_ASSERT(isValid()); return m_data->value; }
68+
T* operator->() const { RELEASE_ASSERT(isValid()); return &m_data->value; }
69+
70+
explicit operator bool() const { return isValid(); }
6271

63-
explicit operator bool() const { return static_cast<bool>(m_data); }
64-
6572
private:
6673
struct Data : ThreadSafeRefCounted<Data> {
6774
template<typename... Arguments>
6875
Data(Arguments&&... arguments)
6976
: value(std::forward<Arguments>(arguments)...)
7077
{
7178
}
72-
79+
7380
T value;
7481
};
7582

Tools/TestWebKitAPI/Tests/WTF/BoxPtr.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,23 @@ void boxPtrLoggerDeleter(BoxPtrLogger* logger)
5050
delete logger;
5151
}
5252

53+
class BoxPtrClassTest {
54+
public:
55+
void method() { ++value; }
56+
int value { 1 };
57+
};
58+
59+
void boxPtrClassTestDeleter(BoxPtrClassTest* theObject)
60+
{
61+
delete theObject;
62+
}
63+
5364
};
5465

5566
namespace WTF {
5667

5768
WTF_DEFINE_BOXPTR_DELETER(TestWebKitAPI::BoxPtrLogger, TestWebKitAPI::boxPtrLoggerDeleter);
69+
WTF_DEFINE_BOXPTR_DELETER(TestWebKitAPI::BoxPtrClassTest, TestWebKitAPI::boxPtrClassTestDeleter);
5870

5971
}
6072

@@ -123,6 +135,14 @@ TEST(WTF_BoxPtr, Basic)
123135
EXPECT_STREQ("create(a) delete(a) ", takeLogStr().c_str());
124136
}
125137

138+
TEST(WTF_BoxPtr, NoCrash)
139+
{
140+
BoxPtr<BoxPtrClassTest> ptr;
141+
EXPECT_EQ(false, static_cast<bool>(ptr));
142+
EXPECT_EQ(false, ptr.isValid());
143+
EXPECT_EQ(nullptr, ptr.get());
144+
}
145+
126146
TEST(WTF_BoxPtr, Assignment)
127147
{
128148
{

0 commit comments

Comments
 (0)