From 8604ba0f1cfc1f2cd73a9d98085143614e587888 Mon Sep 17 00:00:00 2001 From: Rodion Malinovsky Date: Wed, 16 Sep 2015 14:53:12 +0300 Subject: [PATCH 1/3] Add asserts to prevent UB --- include/rapidjson/document.h | 5 ++++- include/rapidjson/internal/stack.h | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index f5c1be9..6275f96 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -1989,7 +1989,10 @@ public: //!@} //! Get the allocator of this document. - Allocator& GetAllocator() { return *allocator_; } + Allocator& GetAllocator() { + RAPIDJSON_ASSERT(allocator_); + return *allocator_; + } //! Get the capacity of stack in bytes. size_t GetStackCapacity() const { return stack_.GetCapacity(); } diff --git a/include/rapidjson/internal/stack.h b/include/rapidjson/internal/stack.h index 0d4a7f7..4b98e38 100644 --- a/include/rapidjson/internal/stack.h +++ b/include/rapidjson/internal/stack.h @@ -134,7 +134,10 @@ public: template T* Bottom() { return (T*)stack_; } - Allocator& GetAllocator() { return *allocator_; } + Allocator& GetAllocator() { + RAPIDJSON_ASSERT(allocator_); + return *allocator_; + } bool Empty() const { return stackTop_ == stack_; } size_t GetSize() const { return static_cast(stackTop_ - stack_); } size_t GetCapacity() const { return static_cast(stackEnd_ - stack_); } From c7433dfc5ede497126ca347af93ce8587beeafb0 Mon Sep 17 00:00:00 2001 From: Rodion Malinovsky Date: Wed, 16 Sep 2015 14:53:51 +0300 Subject: [PATCH 2/3] Add stack::HasAllocator To check is it possible to expose allocator. --- include/rapidjson/internal/stack.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/rapidjson/internal/stack.h b/include/rapidjson/internal/stack.h index 4b98e38..82f23dd 100644 --- a/include/rapidjson/internal/stack.h +++ b/include/rapidjson/internal/stack.h @@ -134,6 +134,10 @@ public: template T* Bottom() { return (T*)stack_; } + bool HasAllocator() const { + return allocator_ != 0; + } + Allocator& GetAllocator() { RAPIDJSON_ASSERT(allocator_); return *allocator_; From 2e11498943153e72afe0af0f64aecf090c0df659 Mon Sep 17 00:00:00 2001 From: Rodion Malinovsky Date: Wed, 16 Sep 2015 14:54:38 +0300 Subject: [PATCH 3/3] Fix the usage of the stack::GetAllocator --- include/rapidjson/document.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 6275f96..0a5a7b2 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -1887,7 +1887,8 @@ public: template GenericDocument& ParseStream(InputStream& is) { ValueType::SetNull(); // Remove existing root if exist - GenericReader reader(&stack_.GetAllocator()); + GenericReader reader( + stack_.HasAllocator() ? &stack_.GetAllocator() : 0); ClearStackOnExit scope(*this); parseResult_ = reader.template Parse(is, *this); if (parseResult_) {