Browse Source

[2831] handle deallocating null

JINMEI Tatuya 12 years ago
parent
commit
c2d22d8155

+ 7 - 1
src/lib/util/memory_segment_mapped.cc

@@ -130,7 +130,13 @@ MemorySegmentMapped::allocate(size_t size) {
 }
 
 void
-MemorySegmentMapped::deallocate(void* ptr, size_t /*size*/) {
+MemorySegmentMapped::deallocate(void* ptr, size_t) {
+    // the underlying deallocate() would deal with the case where ptr == 0,
+    // but it's an undocumented behavior, so we handle it ourselves for safety.
+    if (!ptr) {
+        return;
+    }
+
     impl_->base_sgmt_->deallocate(ptr);
 }
 

+ 3 - 12
src/lib/util/tests/memory_segment_mapped_unittest.cc

@@ -256,19 +256,10 @@ TEST_F(MemorySegmentMappedTest, violateReadOnly) {
         }, "");
 }
 
-/*
-TEST(MemorySegmentLocal, TestNullDeallocate) {
-    auto_ptr<MemorySegment> segment(new MemorySegmentLocal());
-
-    // By default, nothing is allocated.
-    EXPECT_TRUE(segment->allMemoryDeallocated());
-
+TEST_F(MemorySegmentMappedTest, nullDeallocate) {
     // NULL deallocation is a no-op.
-    EXPECT_NO_THROW(segment->deallocate(NULL, 1024));
-
-    // This should still return true.
-    EXPECT_TRUE(segment->allMemoryDeallocated());
+    EXPECT_NO_THROW(segment_->deallocate(0, 1024));
+    EXPECT_TRUE(segment_->allMemoryDeallocated());
 }
-*/
 
 }