Skip to content

Commit 28836b8

Browse files
committed
fix bug for static init
Ugh! Static initialization of instance variables is a very bad idea. This fix is taken from the Chromium code-base. It includes their double-fix for ARM. * https://codereview.chromium.org/24984004 * https://src.chromium.org/viewvc/chrome?revision=226099&view=revision * https://code.google.com/p/webrtc/issues/detail?id=1777
1 parent 47f1577 commit 28836b8

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

include/json/value.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class JSON_API Value {
133133
typedef Json::LargestUInt LargestUInt;
134134
typedef Json::ArrayIndex ArrayIndex;
135135

136-
static const Value null;
136+
static const Value& null;
137137
/// Minimum signed integer value that can be stored in a Json::Value.
138138
static const LargestInt minLargestInt;
139139
/// Maximum signed integer value that can be stored in a Json::Value.

src/lib_json/json_value.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,17 @@
2525

2626
namespace Json {
2727

28-
const Value Value::null;
28+
// This is a walkaround to avoid the static initialization of Value::null.
29+
// kNull must be word-aligned to avoid crashing on ARM. We use an alignment of
30+
// 8 (instead of 4) as a bit of future-proofing.
31+
#if defined(__ARMEL__)
32+
#define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
33+
#else
34+
#define ALIGNAS(byte_alignment)
35+
#endif
36+
static const unsigned char ALIGNAS(8) kNull[sizeof(Value)] = {0};
37+
const Value& Value::null = reinterpret_cast<const Value&>(kNull);
38+
2939
const Int Value::minInt = Int(~(UInt(-1) / 2));
3040
const Int Value::maxInt = Int(UInt(-1) / 2);
3141
const UInt Value::maxUInt = UInt(-1);

0 commit comments

Comments
 (0)