From 8c29a7b4935e5d873a5f8e0a95388db8b35a3777 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 11 Jun 2021 11:49:14 +0800 Subject: [PATCH] Fix Pointer::Append() crash for custom allocator on Windows Fix #1899 --- include/rapidjson/pointer.h | 2 +- test/unittest/pointertest.cpp | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/pointer.h b/include/rapidjson/pointer.h index 90e5903..9621442 100644 --- a/include/rapidjson/pointer.h +++ b/include/rapidjson/pointer.h @@ -163,7 +163,7 @@ public: GenericPointer(const Token* tokens, size_t tokenCount) : allocator_(), ownAllocator_(), nameBuffer_(), tokens_(const_cast(tokens)), tokenCount_(tokenCount), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) {} //! Copy constructor. - GenericPointer(const GenericPointer& rhs) : allocator_(rhs.allocator_), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + GenericPointer(const GenericPointer& rhs) : allocator_(), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { *this = rhs; } diff --git a/test/unittest/pointertest.cpp b/test/unittest/pointertest.cpp index 4371803..50c678e 100644 --- a/test/unittest/pointertest.cpp +++ b/test/unittest/pointertest.cpp @@ -475,7 +475,9 @@ TEST(Pointer, CopyConstructor) { EXPECT_EQ(1u, q.GetTokens()[1].length); EXPECT_STREQ("0", q.GetTokens()[1].name); EXPECT_EQ(0u, q.GetTokens()[1].index); - EXPECT_EQ(&p.GetAllocator(), &q.GetAllocator()); + + // Copied pointer needs to have its own allocator + EXPECT_NE(&p.GetAllocator(), &q.GetAllocator()); } // Static tokens @@ -1668,3 +1670,14 @@ TEST(Pointer, Issue483) { value.SetString(mystr.c_str(), static_cast(mystr.length()), document.GetAllocator()); myjson::Pointer(path.c_str()).Set(document, value, document.GetAllocator()); } + +TEST(Pointer, Issue1899) { + typedef GenericPointer > PointerType; + PointerType p; + PointerType q = p.Append("foo"); + EXPECT_TRUE(PointerType("/foo") == q); + q = q.Append(1234); + EXPECT_TRUE(PointerType("/foo/1234") == q); + q = q.Append(""); + EXPECT_TRUE(PointerType("/foo/1234/") == q); +} \ No newline at end of file