]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2088] Handle NULL deallocate as a nop
authorMukund Sivaraman <muks@isc.org>
Thu, 12 Jul 2012 08:15:49 +0000 (13:45 +0530)
committerMukund Sivaraman <muks@isc.org>
Thu, 12 Jul 2012 08:15:54 +0000 (13:45 +0530)
src/lib/util/memory_segment_local.cc
src/lib/util/tests/memory_segment_local_unittest.cc

index 468c49927020061aad2241ab59382ba7f06bab3a..9c345c9040dfd66a038ecb52c7ca8af3ea2b0553 100644 (file)
@@ -31,6 +31,12 @@ MemorySegmentLocal::allocate(size_t size) {
 
 void
 MemorySegmentLocal::deallocate(void* ptr, size_t size) {
+    if (ptr == NULL) {
+        // Return early if NULL is passed to be deallocated (without
+        // modifying allocated_size, or comparing against it).
+        return;
+    }
+
     if (size > allocated_size_) {
       isc_throw(OutOfRange, "Invalid size to deallocate: " << size
                 << "; currently allocated size: " << allocated_size_);
index e8ee1104d632e1fabcefbf0f6cefb70eb30f2bae..08787ad1e0f11d9fe64b34f389e4360747ccec72 100644 (file)
@@ -86,4 +86,17 @@ TEST(MemorySegmentLocal, TestBadDeallocate) {
     EXPECT_THROW(segment->deallocate(ptr, 2048), isc::OutOfRange);
 }
 
+TEST(MemorySegmentLocal, TestNullDeallocate) {
+    auto_ptr<MemorySegment> segment(new MemorySegmentLocal());
+
+    // By default, nothing is allocated.
+    EXPECT_TRUE(segment->allMemoryDeallocated());
+
+    // NULL deallocation is a no-op.
+    EXPECT_NO_THROW(segment->deallocate(NULL, 1024));
+
+    // This should still return true.
+    EXPECT_TRUE(segment->allMemoryDeallocated());
+}
+
 } // anonymous namespace