summaryrefslogtreecommitdiff
path: root/Tests
diff options
context:
space:
mode:
authorAndrew Kaster <andrewdkaster@gmail.com>2021-05-12 04:46:16 -0600
committerLinus Groh <mail@linusgroh.de>2021-05-14 08:34:00 +0100
commit09fe9f4542bdada7369a19f5c3fc3b9c0b21969f (patch)
treeae7cdb3d5d63d89f1641191aa33b4555357310b9 /Tests
parent28987d1b5638d5b6e88e748d3ca230378a22b87d (diff)
downloadserenity-09fe9f4542bdada7369a19f5c3fc3b9c0b21969f.zip
Tests: Fix use-after-free in TestRefPtr.self_observers
We can't unref an object to destruction while there's still a live RefPtr to the object, otherwise the RefPtr destructor will try to destroy it again, accessing the refcount of a destroyed object (before realizing that oops! the object is already dead)
Diffstat (limited to 'Tests')
-rw-r--r--Tests/AK/TestRefPtr.cpp28
1 files changed, 14 insertions, 14 deletions
diff --git a/Tests/AK/TestRefPtr.cpp b/Tests/AK/TestRefPtr.cpp
index 26615ffba9..995b6fcf1d 100644
--- a/Tests/AK/TestRefPtr.cpp
+++ b/Tests/AK/TestRefPtr.cpp
@@ -129,22 +129,22 @@ TEST_CASE(assign_copy_self)
TEST_CASE(self_observers)
{
- RefPtr<SelfAwareObject> object = adopt_ref(*new SelfAwareObject);
- EXPECT_EQ(object->ref_count(), 1u);
- EXPECT_EQ(object->m_has_one_ref_left, false);
- EXPECT_EQ(SelfAwareObject::num_destroyed, 0u);
-
- object->ref();
- EXPECT_EQ(object->ref_count(), 2u);
- EXPECT_EQ(object->m_has_one_ref_left, false);
- EXPECT_EQ(SelfAwareObject::num_destroyed, 0u);
+ {
+ RefPtr<SelfAwareObject> object = adopt_ref(*new SelfAwareObject);
+ EXPECT_EQ(object->ref_count(), 1u);
+ EXPECT_EQ(object->m_has_one_ref_left, false);
+ EXPECT_EQ(SelfAwareObject::num_destroyed, 0u);
- object->unref();
- EXPECT_EQ(object->ref_count(), 1u);
- EXPECT_EQ(object->m_has_one_ref_left, true);
- EXPECT_EQ(SelfAwareObject::num_destroyed, 0u);
+ object->ref();
+ EXPECT_EQ(object->ref_count(), 2u);
+ EXPECT_EQ(object->m_has_one_ref_left, false);
+ EXPECT_EQ(SelfAwareObject::num_destroyed, 0u);
- object->unref();
+ object->unref();
+ EXPECT_EQ(object->ref_count(), 1u);
+ EXPECT_EQ(object->m_has_one_ref_left, true);
+ EXPECT_EQ(SelfAwareObject::num_destroyed, 0u);
+ }
EXPECT_EQ(SelfAwareObject::num_destroyed, 1u);
}