Commit efe41402 authored by Milo Yip's avatar Milo Yip

Fix #399 MemoryPoolAllocator::Realloc expands fail

parent d43f001a
......@@ -194,6 +194,9 @@ public:
if (newSize == 0)
return NULL;
originalSize = RAPIDJSON_ALIGN(originalSize);
newSize = RAPIDJSON_ALIGN(newSize);
// Do not shrink if new size is smaller than original
if (originalSize >= newSize)
return originalPtr;
......@@ -201,7 +204,6 @@ public:
// Simply expand it if it is the last allocation and there is sufficient space
if (originalPtr == reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) {
size_t increment = static_cast<size_t>(newSize - originalSize);
increment = RAPIDJSON_ALIGN(increment);
if (chunkHead_->size + increment <= chunkHead_->capacity) {
chunkHead_->size += increment;
return originalPtr;
......
......@@ -81,3 +81,22 @@ TEST(Allocator, Alignment) {
}
#endif
}
TEST(Allocator, Issue399) {
MemoryPoolAllocator<> a;
void* p = a.Malloc(100);
void* q = a.Realloc(p, 100, 200);
EXPECT_EQ(p, q);
// exhuasive testing
for (size_t j = 1; j < 32; j++) {
a.Clear();
a.Malloc(j); // some unaligned size
p = a.Malloc(1);
for (size_t i = 1; i < 1024; i++) {
q = a.Realloc(p, i, i + 1);
EXPECT_EQ(p, q);
p = q;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment