From: Marcin Siodelski Date: Wed, 16 Jul 2014 13:02:06 +0000 (+0200) Subject: [3461] Corrected valgrind errors in the util library. X-Git-Tag: trac3482_base~74^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b22d5d37ee0a82063d664006c849402253c0bbfb;p=thirdparty%2Fkea.git [3461] Corrected valgrind errors in the util library. --- diff --git a/src/lib/util/io/fd_share.cc b/src/lib/util/io/fd_share.cc index 866644131a..bae9e2c096 100644 --- a/src/lib/util/io/fd_share.cc +++ b/src/lib/util/io/fd_share.cc @@ -111,7 +111,9 @@ recv_fd(const int sock) { // 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)); + // Only call close() if the descriptor is valid. Otherwise return + // an error. + int close_error(fd >=0 ? close(fd) : -1); 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. diff --git a/src/lib/util/tests/memory_segment_local_unittest.cc b/src/lib/util/tests/memory_segment_local_unittest.cc index 5176c8874c..2f1401199b 100644 --- a/src/lib/util/tests/memory_segment_local_unittest.cc +++ b/src/lib/util/tests/memory_segment_local_unittest.cc @@ -59,7 +59,12 @@ TEST(MemorySegmentLocal, TestLocal) { TEST(MemorySegmentLocal, TestTooMuchMemory) { auto_ptr 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) {