1
0
Fork 0

fix overrun

This commit is contained in:
jsteemann 2018-08-25 11:28:22 +02:00
parent 568a09f177
commit c154dd95ce
1 changed files with 7 additions and 6 deletions

View File

@ -83,7 +83,7 @@ class MemoryBlockAllocator {
public: public:
/// @brief create a temporary storage instance /// @brief create a temporary storage instance
explicit MemoryBlockAllocator(size_t blockSize) explicit MemoryBlockAllocator(size_t blockSize)
: blocks(), blockSize(blockSize), current(nullptr), end(nullptr) {} : blockSize(blockSize), current(nullptr), end(nullptr) {}
/// @brief destroy a temporary storage instance /// @brief destroy a temporary storage instance
~MemoryBlockAllocator() { ~MemoryBlockAllocator() {
@ -99,10 +99,10 @@ class MemoryBlockAllocator {
end = nullptr; end = nullptr;
} }
/// @brief register a short string /// @brief register a short data value
char* store(char const* p, size_t length) { char* store(char const* p, size_t length) {
if (current == nullptr || (current + length > end)) { if (current == nullptr || (current + length > end)) {
allocateBlock(); allocateBlock(length);
} }
TRI_ASSERT(!blocks.empty()); TRI_ASSERT(!blocks.empty());
@ -118,8 +118,9 @@ class MemoryBlockAllocator {
private: private:
/// @brief allocate a new block of memory /// @brief allocate a new block of memory
void allocateBlock() { void allocateBlock(size_t minLength) {
char* buffer = new char[blockSize]; size_t length = std::max(minLength, blockSize);
char* buffer = new char[length];
try { try {
blocks.emplace_back(buffer); blocks.emplace_back(buffer);
@ -128,7 +129,7 @@ class MemoryBlockAllocator {
throw; throw;
} }
current = buffer; current = buffer;
end = current + blockSize; end = current + length;
} }
/// @brief already allocated blocks /// @brief already allocated blocks