Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

fix(asan): pointer used after free in autoref_ptr_test #361

Merged
merged 8 commits into from
Dec 20, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/core/tests/autoref_ptr_test.cpp
Original file line number Diff line number Diff line change
@@ -34,14 +34,12 @@ class Derived : public SelfAssign
class ScopedRefPtrToSelf : public dsn::ref_counter
{
public:
ScopedRefPtrToSelf() : self_ptr_(this) {}
ScopedRefPtrToSelf() {}
vagetablechicken marked this conversation as resolved.
Show resolved Hide resolved

static bool was_destroyed() { return was_destroyed_; }

static void reset_was_destroyed() { was_destroyed_ = false; }

dsn::ref_ptr<ScopedRefPtrToSelf> self_ptr_;

private:
friend class dsn::ref_counter;
~ScopedRefPtrToSelf() { was_destroyed_ = true; }
@@ -152,22 +150,22 @@ TEST(RefCountedUnitTest, ScopedRefPtrToSelfPointerAssignment)
{
ScopedRefPtrToSelf::reset_was_destroyed();

ScopedRefPtrToSelf *check = new ScopedRefPtrToSelf();
dsn::ref_ptr<ScopedRefPtrToSelf> check(new ScopedRefPtrToSelf());
EXPECT_FALSE(ScopedRefPtrToSelf::was_destroyed());
check->self_ptr_ = nullptr;
check = nullptr;
EXPECT_TRUE(ScopedRefPtrToSelf::was_destroyed());
}

TEST(RefCountedUnitTest, ScopedRefPtrToSelfMoveAssignment)
{
ScopedRefPtrToSelf::reset_was_destroyed();

ScopedRefPtrToSelf *check = new ScopedRefPtrToSelf();
dsn::ref_ptr<ScopedRefPtrToSelf> check(new ScopedRefPtrToSelf());
EXPECT_FALSE(ScopedRefPtrToSelf::was_destroyed());
// Releasing |check->self_ptr_| will delete |check|.
// The move assignment operator must assign |check->self_ptr_| first then
// release |check->self_ptr_|.
check->self_ptr_ = dsn::ref_ptr<ScopedRefPtrToSelf>();
check = dsn::ref_ptr<ScopedRefPtrToSelf>();
EXPECT_TRUE(ScopedRefPtrToSelf::was_destroyed());
}