Skip to content

Commit 7f0399d

Browse files
hchunhuicdunn2001
authored andcommitted
Use special constructor in order to omit "dereferencing type-punned pointer" error.
1 parent c70e190 commit 7f0399d

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

include/json/value.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ Json::Value arr_value(Json::arrayValue); // []
249249
Json::Value obj_value(Json::objectValue); // {}
250250
\endcode
251251
*/
252+
struct StaticInitTag {};
253+
Value(StaticInitTag);
252254
Value(ValueType type = nullValue);
253255
Value(Int value);
254256
Value(UInt value);

src/lib_json/json_value.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ namespace Json {
3333
const Value Value::null;
3434
#define ALIGNAS(byte_alignment)
3535
#endif
36-
static const unsigned char ALIGNAS(8) kNull[sizeof(Value)] = { 0 };
37-
const Value& Value::null = reinterpret_cast<const Value&>(kNull[0]);
38-
const Value& Value::nullRef = reinterpret_cast<const Value&>(kNull[0]);
36+
static const Value kNull((Value::StaticInitTag()));
37+
const Value& Value::null = kNull;
38+
const Value& Value::nullRef = kNull;
3939

4040
const Int Value::minInt = Int(~(UInt(-1) / 2));
4141
const Int Value::maxInt = Int(UInt(-1) / 2);
@@ -309,6 +309,8 @@ bool Value::CZString::isStaticString() const { return storage_.policy_ == noDupl
309309
// //////////////////////////////////////////////////////////////////
310310
// //////////////////////////////////////////////////////////////////
311311

312+
Value::Value(Value::StaticInitTag) {}
313+
312314
/*! \internal Default constructor initialization must be equivalent to:
313315
* memset( this, 0, sizeof(Value) )
314316
* This optimization is used in ValueInternalMap fast allocator.

0 commit comments

Comments
 (0)