Skip to content

Commit 1f73267

Browse files
dawesccdunn2001
dawesc
authored andcommitted
More
1 parent d30d10b commit 1f73267

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

Diff for: src/jsontestrunner/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static int parseAndSaveValueTree(const JSONCPP_STRING& input,
151151
Json::Value* root)
152152
{
153153
Json::Reader reader(features);
154-
bool parsingSuccessful = reader.parse(input, *root);
154+
bool parsingSuccessful = reader.parse(input.data(), input.data() + input.size(), *root);
155155
if (!parsingSuccessful) {
156156
printf("Failed to parse %s file: \n%s\n",
157157
kind.c_str(),

Diff for: src/lib_json/json_reader.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ Reader::Reader(const Features& features)
9898

9999
bool
100100
Reader::parse(const std::string& document, Value& root, bool collectComments) {
101-
document_ = document;
101+
JSONCPP_STRING documentCopy(document.data(), document.data() + document.capacity());
102+
std::swap(documentCopy, document_);
102103
const char* begin = document_.c_str();
103104
const char* end = begin + document_.length();
104105
return parse(begin, end, root, collectComments);
@@ -114,7 +115,7 @@ bool Reader::parse(std::istream& sin, Value& root, bool collectComments) {
114115
// create an extra copy.
115116
JSONCPP_STRING doc;
116117
std::getline(sin, doc, (char)EOF);
117-
return parse(doc, root, collectComments);
118+
return parse(doc.data(), doc.data() + doc.size(), root, collectComments);
118119
}
119120

120121
bool Reader::parse(const char* beginDoc,

Diff for: src/lib_json/json_value.cpp

+48-10
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,25 @@ inline static void decodePrefixedString(
138138
}
139139
/** Free the string duplicated by duplicateStringValue()/duplicateAndPrefixStringValue().
140140
*/
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+
}
142160

143161
} // namespace Json
144162

@@ -193,12 +211,12 @@ Value::CommentInfo::CommentInfo() : comment_(0)
193211

194212
Value::CommentInfo::~CommentInfo() {
195213
if (comment_)
196-
releaseStringValue(comment_);
214+
releaseStringValue(comment_, 0u);
197215
}
198216

199217
void Value::CommentInfo::setComment(const char* text, size_t len) {
200218
if (comment_) {
201-
releaseStringValue(comment_);
219+
releaseStringValue(comment_, 0u);
202220
comment_ = 0;
203221
}
204222
JSON_ASSERT(text != 0);
@@ -229,10 +247,10 @@ Value::CZString::CZString(char const* str, unsigned ulength, DuplicationPolicy a
229247
storage_.length_ = ulength & 0x3FFFFFFF;
230248
}
231249

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_);
236254
storage_.policy_ = static_cast<unsigned>(other.cstr_
237255
? (static_cast<DuplicationPolicy>(other.storage_.policy_) == noDuplication
238256
? noDuplication : duplicate)
@@ -248,8 +266,14 @@ Value::CZString::CZString(CZString&& other)
248266
#endif
249267

250268
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+
}
253277
}
254278

255279
void Value::CZString::swap(CZString& other) {
@@ -455,7 +479,7 @@ Value::~Value() {
455479
break;
456480
case stringValue:
457481
if (allocated_)
458-
releaseStringValue(value_.string_);
482+
releasePrefixedStringValue(value_.string_);
459483
break;
460484
case arrayValue:
461485
case objectValue:
@@ -467,6 +491,8 @@ Value::~Value() {
467491

468492
if (comments_)
469493
delete[] comments_;
494+
495+
value_.uint_ = 0;
470496
}
471497

472498
Value& Value::operator=(Value other) {
@@ -611,6 +637,18 @@ const char* Value::asCString() const {
611637
return this_str;
612638
}
613639

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+
614652
bool Value::getString(char const** str, char const** cend) const {
615653
if (type_ != stringValue) return false;
616654
if (value_.string_ == 0) return false;

0 commit comments

Comments
 (0)