Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/miloyip/rapidjson.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp A. Hartmann <pah@qo.cx>2015-04-16 22:05:08 +0300
committerPhilipp A. Hartmann <pah@qo.cx>2015-04-16 22:05:08 +0300
commit0c5c1538dcfc7f160e5a4aa208ddf092c787be5a (patch)
tree79bd73cd89f4c4b94cfbe125f499ee9fffa03d9a /include/rapidjson/allocators.h
parent94c0082e38c55b2acf8aa9848125d20692808d45 (diff)
Avoid calling memcpy with NULL pointers
According to the C/C++ standards, calling `memcpy(NULL, NULL, 0)` is undefined behaviour. Recent GCC versions may rely on this by optimizing NULL pointer checks more aggressively, see [1]. This patch tries to avoid calling std::memcpy with zero elements. As a side effect, explicitly return NULL when requesting an empty block from MemoryPoolAllocator::Malloc. This may be related to #301. [1] https://gcc.gnu.org/gcc-4.9/porting_to.html
Diffstat (limited to 'include/rapidjson/allocators.h')
-rw-r--r--include/rapidjson/allocators.h7
1 files changed, 6 insertions, 1 deletions
diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h
index 7b348b6b..d3dcb122 100644
--- a/include/rapidjson/allocators.h
+++ b/include/rapidjson/allocators.h
@@ -160,6 +160,9 @@ public:
//! Allocates a memory block. (concept Allocator)
void* Malloc(size_t size) {
+ if (!size)
+ return NULL;
+
size = RAPIDJSON_ALIGN(size);
if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
@@ -191,7 +194,9 @@ public:
// Realloc process: allocate and copy memory, do not free original buffer.
void* newBuffer = Malloc(newSize);
RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly.
- return std::memcpy(newBuffer, originalPtr, originalSize);
+ if (originalSize)
+ std::memcpy(newBuffer, originalPtr, originalSize);
+ return newBuffer;
}
//! Frees a memory block (concept Allocator)