Browse Source

[master] Merge branch 'trac3461'

Marcin Siodelski 10 years ago
parent
commit
003533bf6e
2 changed files with 16 additions and 7 deletions
  1. 10 6
      src/lib/util/io/fd_share.cc
  2. 6 1
      src/lib/util/tests/memory_segment_local_unittest.cc

+ 10 - 6
src/lib/util/io/fd_share.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2010, 2014 Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -107,11 +107,15 @@ recv_fd(const int sock) {
         std::memcpy(&fd, CMSG_DATA(cmsg), sizeof(int));
     }
     free(msghdr.msg_control);
-    // It is strange, but the call can return the same file descriptor as
-    // one returned previously, even if that one is not closed yet. So,
-    // we just re-number every one we get, so they are unique.
-    int new_fd(dup(fd));
-    int close_error(close(fd));
+    int new_fd = -1;
+    int close_error = -1;
+    if (fd >= 0) {
+        // It is strange, but the call can return the same file descriptor as
+        // one returned previously, even if that one is not closed yet. So,
+        // we just re-number every one we get, so they are unique.
+        new_fd = dup(fd);
+        close_error = close(fd);
+    }
     if (close_error == -1 || new_fd == -1) {
         // We need to return an error, because something failed. But in case
         // it was the previous close, we at least try to close the duped FD.

+ 6 - 1
src/lib/util/tests/memory_segment_local_unittest.cc

@@ -59,7 +59,12 @@ TEST(MemorySegmentLocal, TestLocal) {
 TEST(MemorySegmentLocal, TestTooMuchMemory) {
     auto_ptr<MemorySegment> segment(new MemorySegmentLocal());
 
-    EXPECT_THROW(segment->allocate(ULONG_MAX), bad_alloc);
+    // Although it should be perfectly fine to use the ULONG_MAX
+    // instead of LONG_MAX as the size_t value should be unsigned,
+    // Valgrind appears to be using the signed value and hence the
+    // maximum positive value is LONG_MAX for Valgrind. But, this
+    // should be sufficient to test the "too much memory" conditions.
+    EXPECT_THROW(segment->allocate(LONG_MAX), bad_alloc);
 }
 
 TEST(MemorySegmentLocal, TestBadDeallocate) {