From 0a56e6496f88596ca483f49630187aedbb5990d5 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 20 Jun 2014 16:18:08 +0800 Subject: [PATCH] GenericValue: avoid memset/memcpy(this,...) To avoid writing outside of the current GenericValue object and to follow the C++ standard more closely, this patch drops the memset/memcpy(this,...) occurences in GenericValue in favour of explicit initialisations/assignments of the data_ and flag_ members. https://code.google.com/p/rapidjson/issues/detail?id=11 https://github.com/pah/rapidjson/commit/7475a969 --- include/rapidjson/document.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index a41288e..5524241 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -60,14 +60,13 @@ public: \param type Type of the value. \note Default content for number is zero. */ - GenericValue(Type type) { + GenericValue(Type type) : data_() { static const unsigned defaultFlags[7] = { kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kConstStringFlag, kNumberFlag | kIntFlag | kUintFlag | kInt64Flag | kUint64Flag | kDoubleFlag }; RAPIDJSON_ASSERT(type <= kNumberType); flags_ = defaultFlags[type]; - memset(&data_, 0, sizeof(data_)); } //! Constructor for boolean value. @@ -170,8 +169,7 @@ public: GenericValue& operator=(GenericValue& rhs) { RAPIDJSON_ASSERT(this != &rhs); this->~GenericValue(); - memcpy(this, &rhs, sizeof(GenericValue)); - rhs.flags_ = kNullFlag; + RawAssign(rhs); return *this; } @@ -677,7 +675,8 @@ private: //! Assignment without calling destructor void RawAssign(GenericValue& rhs) { - memcpy(this, &rhs, sizeof(GenericValue)); + data_ = rhs.data_; + flags_ = rhs.flags_; rhs.flags_ = kNullFlag; }