CBL-Mariner/SPECS/rapidjson/0002-Correct-object-copying...

51 lines
2.0 KiB
Diff

diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h
index e3e20dfb..95aa8999 100644
--- a/include/rapidjson/document.h
+++ b/include/rapidjson/document.h
@@ -22,6 +22,7 @@
#include "internal/strfunc.h"
#include "memorystream.h"
#include "encodedstream.h"
+#include <algorithm>
#include <new> // placement new
#include <limits>
@@ -69,6 +70,7 @@ class GenericDocument;
*/
template <typename Encoding, typename Allocator>
struct GenericMember {
+ GenericMember() {}
GenericValue<Encoding, Allocator> name; //!< name of member (must be a string)
GenericValue<Encoding, Allocator> value; //!< value of member.
};
@@ -1934,9 +1936,11 @@ private:
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(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);
@@ -1947,9 +1951,11 @@ private:
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(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);