Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct object copying in document.h #1698

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 15 additions & 6 deletions include/rapidjson/document.h
Expand Up @@ -22,6 +22,8 @@
#include "internal/strfunc.h"
#include "memorystream.h"
#include "encodedstream.h"

#include <algorithm>
#include <new> // placement new
#include <limits>
#ifdef __cpp_lib_three_way_comparison
Expand Down Expand Up @@ -110,6 +112,9 @@ class GenericDocument;
template <typename Encoding, typename Allocator>
class GenericMember {
public:
// Allow default construction as it is needed during copying.
GenericMember() {}

GenericValue<Encoding, Allocator> name; //!< name of member (must be a string)
GenericValue<Encoding, Allocator> value; //!< value of member.

Expand Down Expand Up @@ -2113,9 +2118,11 @@ class GenericValue {
void SetArrayRaw(GenericValue* values, SizeType count, Allocator& allocator) {
data_.f.flags = kArrayFlag;
if (count) {
GenericValue* e = static_cast<GenericValue*>(allocator.Malloc(count * sizeof(GenericValue)));
SetElementsPointer(e);
std::memcpy(static_cast<void*>(e), values, count * sizeof(GenericValue));
auto arr = static_cast<GenericValue*>(allocator.Malloc(count * sizeof(GenericValue)));
for (SizeType idx = 0; idx < count; ++idx)
new (arr + idx) GenericValue;
SetElementsPointer(arr);
std::copy_n(values, count, arr);
}
else
SetElementsPointer(0);
Expand All @@ -2126,9 +2133,11 @@ class GenericValue {
void SetObjectRaw(Member* members, SizeType count, Allocator& allocator) {
data_.f.flags = kObjectFlag;
if (count) {
Member* m = static_cast<Member*>(allocator.Malloc(count * sizeof(Member)));
SetMembersPointer(m);
std::memcpy(static_cast<void*>(m), members, count * sizeof(Member));
auto arr = static_cast<Member*>(allocator.Malloc(count * sizeof(Member)));
for (SizeType idx = 0; idx < count; ++idx)
new (arr + idx) Member;
SetMembersPointer(arr);
std::copy_n(members, count, arr);
}
else
SetMembersPointer(0);
Expand Down