@@ -138,7 +138,29 @@ inline static void decodePrefixedString(
138
138
}
139
139
/* * Free the string duplicated by duplicateStringValue()/duplicateAndPrefixStringValue().
140
140
*/
141
- static inline void releaseStringValue (char * value) { free (value); }
141
+ #if JSONCPP_USING_SECURE_MEMORY
142
+ static inline void releasePrefixedStringValue (char * value) {
143
+ unsigned length = 0 ;
144
+ char const * valueDecoded;
145
+ decodePrefixedString (true , value, &length, &valueDecoded);
146
+ size_t const size = sizeof (unsigned ) + length + 1U ;
147
+ memset (value, 0 , size);
148
+ free (value);
149
+ }
150
+ static inline void releaseStringValue (char * value, unsigned length) {
151
+ // length==0 => we allocated the strings memory
152
+ size_t size = (length==0 ) ? strlen (value) : length;
153
+ memset (value, 0 , size);
154
+ free (value);
155
+ }
156
+ #else // !JSONCPP_USING_SECURE_MEMORY
157
+ static inline void releasePrefixedStringValue (char * value) {
158
+ free (value);
159
+ }
160
+ static inline void releaseStringValue (char * value, unsigned length) {
161
+ free (value);
162
+ }
163
+ #endif // JSONCPP_USING_SECURE_MEMORY
142
164
143
165
} // namespace Json
144
166
@@ -193,12 +215,12 @@ Value::CommentInfo::CommentInfo() : comment_(0)
193
215
194
216
Value::CommentInfo::~CommentInfo () {
195
217
if (comment_)
196
- releaseStringValue (comment_);
218
+ releaseStringValue (comment_, 0u );
197
219
}
198
220
199
221
void Value::CommentInfo::setComment (const char * text, size_t len) {
200
222
if (comment_) {
201
- releaseStringValue (comment_);
223
+ releaseStringValue (comment_, 0u );
202
224
comment_ = 0 ;
203
225
}
204
226
JSON_ASSERT (text != 0 );
@@ -229,10 +251,10 @@ Value::CZString::CZString(char const* str, unsigned ulength, DuplicationPolicy a
229
251
storage_.length_ = ulength & 0x3FFFFFFF ;
230
252
}
231
253
232
- Value::CZString::CZString (const CZString& other)
233
- : cstr_ (other.storage_.policy_ != noDuplication && other.cstr_ != 0
234
- ? duplicateStringValue(other.cstr_, other.storage_.length_)
235
- : other.cstr_) {
254
+ Value::CZString::CZString (const CZString& other) {
255
+ cstr_ = (other.storage_ .policy_ != noDuplication && other.cstr_ != 0
256
+ ? duplicateStringValue (other.cstr_ , other.storage_ .length_ )
257
+ : other.cstr_ );
236
258
storage_.policy_ = static_cast <unsigned >(other.cstr_
237
259
? (static_cast <DuplicationPolicy>(other.storage_ .policy_ ) == noDuplication
238
260
? noDuplication : duplicate)
@@ -248,8 +270,9 @@ Value::CZString::CZString(CZString&& other)
248
270
#endif
249
271
250
272
Value::CZString::~CZString () {
251
- if (cstr_ && storage_.policy_ == duplicate)
252
- releaseStringValue (const_cast <char *>(cstr_));
273
+ if (cstr_ && storage_.policy_ == duplicate) {
274
+ releaseStringValue (const_cast <char *>(cstr_), storage_.length_ + 1u ); // +1 for null terminating character for sake of completeness but not actually necessary
275
+ }
253
276
}
254
277
255
278
void Value::CZString::swap (CZString& other) {
@@ -455,7 +478,7 @@ Value::~Value() {
455
478
break ;
456
479
case stringValue:
457
480
if (allocated_)
458
- releaseStringValue (value_.string_ );
481
+ releasePrefixedStringValue (value_.string_ );
459
482
break ;
460
483
case arrayValue:
461
484
case objectValue:
@@ -467,6 +490,8 @@ Value::~Value() {
467
490
468
491
if (comments_)
469
492
delete[] comments_;
493
+
494
+ value_.uint_ = 0 ;
470
495
}
471
496
472
497
Value& Value::operator =(Value other) {
@@ -611,6 +636,18 @@ const char* Value::asCString() const {
611
636
return this_str;
612
637
}
613
638
639
+ #if JSONCPP_USING_SECURE_MEMORY
640
+ unsigned Value::getCStringLength () const {
641
+ JSON_ASSERT_MESSAGE (type_ == stringValue,
642
+ " in Json::Value::asCString(): requires stringValue" );
643
+ if (value_.string_ == 0 ) return 0 ;
644
+ unsigned this_len;
645
+ char const * this_str;
646
+ decodePrefixedString (this ->allocated_ , this ->value_ .string_ , &this_len, &this_str);
647
+ return this_len;
648
+ }
649
+ #endif
650
+
614
651
bool Value::getString (char const ** str, char const ** cend) const {
615
652
if (type_ != stringValue) return false ;
616
653
if (value_.string_ == 0 ) return false ;
0 commit comments