summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorshess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-13 23:30:20 +0000
committershess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-13 23:30:20 +0000
commit042070d932d4689d2f2e7907348ecd3b52aba3be (patch)
tree9d614c35906056b5e666fd59b81c07555cafc39f /chrome
parent23576484b6f85938f72a1c993bfdf754b0c54660 (diff)
downloadchromium_src-042070d932d4689d2f2e7907348ecd3b52aba3be.zip
chromium_src-042070d932d4689d2f2e7907348ecd3b52aba3be.tar.gz
chromium_src-042070d932d4689d2f2e7907348ecd3b52aba3be.tar.bz2
Fix valigrind complaint about invalid file descriptor on close.
Fixes FileDescriptorSet.BasicAddAndClose and FileDescriptorSet.SetDescriptors. http://crbug.com/11724 Review URL: http://codereview.chromium.org/115326 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16014 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/common/file_descriptor_set_unittest.cc44
1 files changed, 32 insertions, 12 deletions
diff --git a/chrome/common/file_descriptor_set_unittest.cc b/chrome/common/file_descriptor_set_unittest.cc
index 7cd9d5f..f412fdd 100644
--- a/chrome/common/file_descriptor_set_unittest.cc
+++ b/chrome/common/file_descriptor_set_unittest.cc
@@ -12,6 +12,24 @@
#include "chrome/common/file_descriptor_set_posix.h"
#include "testing/gtest/include/gtest/gtest.h"
+namespace {
+
+// Get a safe file descriptor for test purposes.
+int GetSafeFd() {
+ return open("/dev/null", O_RDONLY);
+}
+
+// Returns true if fd was already closed. Closes fd if not closed.
+bool VerifyClosed(int fd) {
+ const int duped = dup(fd);
+ if (duped != -1) {
+ HANDLE_EINTR(close(duped));
+ HANDLE_EINTR(close(fd));
+ return false;
+ }
+ return true;
+}
+
// The FileDescriptorSet will try and close some of the descriptor numbers
// which we given it. This is the base descriptor value. It's great enough such
// that no real descriptor will accidently be closed.
@@ -36,13 +54,15 @@ TEST(FileDescriptorSet, BasicAddAndClose) {
ASSERT_EQ(set->size(), 0u);
ASSERT_TRUE(set->empty());
- ASSERT_TRUE(set->AddAndAutoClose(kFDBase));
+ const int fd = GetSafeFd();
+ ASSERT_TRUE(set->AddAndAutoClose(fd));
ASSERT_EQ(set->size(), 1u);
ASSERT_TRUE(!set->empty());
set->CommitAll();
-}
+ ASSERT_TRUE(VerifyClosed(fd));
+}
TEST(FileDescriptorSet, MaxSize) {
scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
@@ -63,12 +83,15 @@ TEST(FileDescriptorSet, SetDescriptors) {
set->SetDescriptors(NULL, 0);
ASSERT_TRUE(set->empty());
- static const int fds[] = {kFDBase};
+ const int fd = GetSafeFd();
+ static const int fds[] = {fd};
set->SetDescriptors(fds, 1);
ASSERT_TRUE(!set->empty());
ASSERT_EQ(set->size(), 1u);
set->CommitAll();
+
+ ASSERT_TRUE(VerifyClosed(fd));
}
TEST(FileDescriptorSet, GetDescriptors) {
@@ -135,24 +158,21 @@ TEST(FileDescriptorSet, WalkCycle) {
TEST(FileDescriptorSet, DontClose) {
scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
- const int fd = open("/dev/null", O_RDONLY);
+ const int fd = GetSafeFd();
ASSERT_TRUE(set->Add(fd));
set->CommitAll();
- const int duped = dup(fd);
- ASSERT_GE(duped, 0);
- HANDLE_EINTR(close(duped));
- HANDLE_EINTR(close(fd));
+ ASSERT_FALSE(VerifyClosed(fd));
}
TEST(FileDescriptorSet, DoClose) {
scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
- const int fd = open("/dev/null", O_RDONLY);
+ const int fd = GetSafeFd();
ASSERT_TRUE(set->AddAndAutoClose(fd));
set->CommitAll();
- const int duped = dup(fd);
- ASSERT_EQ(duped, -1);
- HANDLE_EINTR(close(fd));
+ ASSERT_TRUE(VerifyClosed(fd));
}
+
+} // namespace