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