From f157a5651c79a7a36e242af216a5d5b383ba8af2 Mon Sep 17 00:00:00 2001 From: Feng Xiao Date: Fri, 14 Nov 2014 11:50:31 -0800 Subject: Down-integrate from internal code base (C++ maps support). --- src/google/protobuf/arena_unittest.cc | 41 +++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'src/google/protobuf/arena_unittest.cc') diff --git a/src/google/protobuf/arena_unittest.cc b/src/google/protobuf/arena_unittest.cc index 7ab6fad8..b2281817 100644 --- a/src/google/protobuf/arena_unittest.cc +++ b/src/google/protobuf/arena_unittest.cc @@ -30,14 +30,26 @@ #include +#include +#include +#include +#ifndef _SHARED_PTR_H +#include +#endif #include #include +#include #include +#include #include #include -#include -#include +#include +#include +#include +#include +#include +#include #include @@ -110,6 +122,31 @@ TEST(ArenaTest, BasicCreate) { EXPECT_EQ(2, notifier.GetCount()); } +TEST(ArenaTest, InitialBlockTooSmall) { + // Construct a small (64 byte) initial block of memory to be used by the + // arena allocator; then, allocate an object which will not fit in the + // initial block. + std::vector arena_block(64); + ArenaOptions options; + options.initial_block = arena_block.data(); + options.initial_block_size = arena_block.size(); + Arena arena(options); + + char* p = ::google::protobuf::Arena::CreateArray(&arena, 96); + uintptr_t allocation = reinterpret_cast(p); + + // Ensure that the arena allocator did not return memory pointing into the + // initial block of memory. + uintptr_t arena_start = reinterpret_cast(arena_block.data()); + uintptr_t arena_end = arena_start + arena_block.size(); + EXPECT_FALSE(allocation >= arena_start && allocation < arena_end); + + // Write to the memory we allocated; this should (but is not guaranteed to) + // trigger a check for heap corruption if the object was allocated from the + // initially-provided block. + memset(p, '\0', 128); +} + TEST(ArenaTest, Parsing) { TestAllTypes original; TestUtil::SetAllFields(&original); -- cgit v1.2.3