BIND 10 trac2088, updated. c0af8f8c458f85e5771a7dd47e17ab8c45a17d07 [2088] Throw isc::OutOfRange when size passed to deallocate() is larger than what was allocated
BIND 10 source code commits
bind10-changes at lists.isc.org
Thu Jul 12 05:02:30 UTC 2012
The branch, trac2088 has been updated
via c0af8f8c458f85e5771a7dd47e17ab8c45a17d07 (commit)
from aa584b6a81609f94620b538329977d1f62ad702a (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit c0af8f8c458f85e5771a7dd47e17ab8c45a17d07
Author: Mukund Sivaraman <muks at isc.org>
Date: Thu Jul 12 10:31:44 2012 +0530
[2088] Throw isc::OutOfRange when size passed to deallocate() is larger than what was allocated
-----------------------------------------------------------------------
Summary of changes:
src/lib/util/memory_segment.h | 3 +++
src/lib/util/memory_segment_local.cc | 8 +++++-
src/lib/util/memory_segment_local.h | 3 +++
.../util/tests/memory_segment_local_unittest.cc | 28 ++++++++++++++++++++
4 files changed, 41 insertions(+), 1 deletion(-)
-----------------------------------------------------------------------
diff --git a/src/lib/util/memory_segment.h b/src/lib/util/memory_segment.h
index 523fc76..d8b718a 100644
--- a/src/lib/util/memory_segment.h
+++ b/src/lib/util/memory_segment.h
@@ -43,6 +43,9 @@ public:
/// \brief Free/release a segment of memory.
///
+ /// This method may throw <code>isc::OutOfRange</code> if \c size is
+ /// not equal to the originally allocated size.
+ ///
/// \param ptr Pointer to the block of memory to free/release. This
/// should be equal to a value returned by <code>allocate()</code>.
/// \param size The size of the memory to be freed in bytes. This
diff --git a/src/lib/util/memory_segment_local.cc b/src/lib/util/memory_segment_local.cc
index 8e89d74..71f7f2c 100644
--- a/src/lib/util/memory_segment_local.cc
+++ b/src/lib/util/memory_segment_local.cc
@@ -13,7 +13,8 @@
// PERFORMANCE OF THIS SOFTWARE.
#include "memory_segment_local.h"
-#include <new>
+#include <exceptions/exceptions.h>
+//#include <new>
namespace isc {
namespace util {
@@ -31,6 +32,11 @@ MemorySegmentLocal::allocate(size_t size) {
void
MemorySegmentLocal::deallocate(void* ptr, size_t size) {
+ if (size > allocated_size_) {
+ isc_throw(OutOfRange, "Invalid size to deallocate: " << size
+ << "; currently allocated size: " << allocated_size_);
+ }
+
allocated_size_ -= size;
free(ptr);
}
diff --git a/src/lib/util/memory_segment_local.h b/src/lib/util/memory_segment_local.h
index 7833545..5e43e53 100644
--- a/src/lib/util/memory_segment_local.h
+++ b/src/lib/util/memory_segment_local.h
@@ -48,6 +48,9 @@ public:
/// \brief Free/release a segment of memory.
///
+ /// This method may throw <code>isc::OutOfRange</code> if \c size is
+ /// not equal to the originally allocated size.
+ ///
/// \param ptr Pointer to the block of memory to free/release. This
/// should be equal to a value returned by <code>allocate()</code>.
/// \param size The size of the memory to be freed in bytes. This
diff --git a/src/lib/util/tests/memory_segment_local_unittest.cc b/src/lib/util/tests/memory_segment_local_unittest.cc
index 8875c3a..e8ee110 100644
--- a/src/lib/util/tests/memory_segment_local_unittest.cc
+++ b/src/lib/util/tests/memory_segment_local_unittest.cc
@@ -13,6 +13,7 @@
// PERFORMANCE OF THIS SOFTWARE.
#include "util/memory_segment_local.h"
+#include <exceptions/exceptions.h>
#include <gtest/gtest.h>
#include <memory>
@@ -58,4 +59,31 @@ TEST(MemorySegmentLocal, TestTooMuchMemory) {
EXPECT_THROW(segment->allocate(0x7fffffffffffffff), bad_alloc);
}
+TEST(MemorySegmentLocal, TestBadDeallocate) {
+ auto_ptr<MemorySegment> segment(new MemorySegmentLocal());
+
+ // By default, nothing is allocated.
+ EXPECT_TRUE(segment->allMemoryDeallocated());
+
+ void* ptr = segment->allocate(1024);
+
+ // Now, we have an allocation:
+ EXPECT_FALSE(segment->allMemoryDeallocated());
+
+ // This should not throw
+ EXPECT_NO_THROW(segment->deallocate(ptr, 1024));
+
+ // Now, we have an deallocated everything:
+ EXPECT_TRUE(segment->allMemoryDeallocated());
+
+ ptr = segment->allocate(1024);
+
+ // Now, we have another allocation:
+ EXPECT_FALSE(segment->allMemoryDeallocated());
+
+ // This should throw as the size passed to deallocate() is larger
+ // than what was allocated.
+ EXPECT_THROW(segment->deallocate(ptr, 2048), isc::OutOfRange);
+}
+
} // anonymous namespace
More information about the bind10-changes
mailing list