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)
This commit is contained in:
Andrew Kaster 2021-05-12 04:46:16 -06:00 committed by Linus Groh
parent 28987d1b56
commit 09fe9f4542
Notes: sideshowbarker 2024-07-18 18:11:23 +09:00

View file

@ -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);
{
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);
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();
EXPECT_EQ(object->ref_count(), 1u);
EXPECT_EQ(object->m_has_one_ref_left, true);
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);
}