Skip to content

Commit

Permalink
mojo: Don't leak handles when assigning to ScopedHandles.
Browse files Browse the repository at this point in the history
BUG=374520

Review URL: https://codereview.chromium.org/336313003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278015 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
erg@chromium.org committed Jun 18, 2014
1 parent 33d6649 commit 493e2e8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mojo/public/cpp/system/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ class ScopedHandleBase {
// Move-only constructor and operator=.
ScopedHandleBase(RValue other) : handle_(other.object->release()) {}
ScopedHandleBase& operator=(RValue other) {
handle_ = other.object->release();
if (other.object != this) {
CloseIfNecessary();
handle_ = other.object->release();
}
return *this;
}

Expand Down
27 changes: 27 additions & 0 deletions mojo/public/cpp/system/tests/core_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,33 @@ TEST(CoreCppTest, TearDownWithMessagesEnqueued) {
}
}

TEST(CoreCppTest, ScopedHandleMoveCtor) {
ScopedSharedBufferHandle buffer1;
EXPECT_EQ(MOJO_RESULT_OK, CreateSharedBuffer(NULL, 1024, &buffer1));
EXPECT_TRUE(buffer1.is_valid());

ScopedSharedBufferHandle buffer2;
EXPECT_EQ(MOJO_RESULT_OK, CreateSharedBuffer(NULL, 1024, &buffer2));
EXPECT_TRUE(buffer2.is_valid());

// If this fails to close buffer1, ScopedHandleBase::CloseIfNecessary() will
// assert.
buffer1 = buffer2.Pass();

EXPECT_TRUE(buffer1.is_valid());
EXPECT_FALSE(buffer2.is_valid());
}

TEST(CoreCppTest, ScopedHandleMoveCtorSelf) {
ScopedSharedBufferHandle buffer1;
EXPECT_EQ(MOJO_RESULT_OK, CreateSharedBuffer(NULL, 1024, &buffer1));
EXPECT_TRUE(buffer1.is_valid());

buffer1 = buffer1.Pass();

EXPECT_TRUE(buffer1.is_valid());
}

// TODO(vtl): Write data pipe tests.

} // namespace
Expand Down

0 comments on commit 493e2e8

Please sign in to comment.