mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-10-17 19:25:52 +02:00
-DJSONCPP_USE_SECURE_MEMORY=1 for cmake
Add allocator.h to amalgamated header Test JSONCPP_USE_SECURE_MEMORY in Travis
This commit is contained in:
@@ -33,9 +33,6 @@ static JSONCPP_STRING normalizeFloatingPointStr(double value) {
|
||||
#endif
|
||||
buffer[sizeof(buffer) - 1] = 0;
|
||||
JSONCPP_STRING s(buffer);
|
||||
#if JSON_USE_SECURE_MEMORY
|
||||
memset(&buffer, 0, sizeof(buffer));
|
||||
#endif
|
||||
JSONCPP_STRING::size_type index = s.find_last_of("eE");
|
||||
if (index != JSONCPP_STRING::npos) {
|
||||
JSONCPP_STRING::size_type hasSign =
|
||||
@@ -69,9 +66,6 @@ static JSONCPP_STRING readInputTestFile(const char* path) {
|
||||
if (fread(buffer, 1, usize, file) == usize)
|
||||
text = buffer;
|
||||
fclose(file);
|
||||
#if JSON_USE_SECURE_MEMORY
|
||||
memset(buffer, 0, static_cast<size_t>(size + 1));
|
||||
#endif
|
||||
delete[] buffer;
|
||||
return text;
|
||||
}
|
||||
@@ -151,7 +145,7 @@ static int parseAndSaveValueTree(const JSONCPP_STRING& input,
|
||||
Json::Value* root)
|
||||
{
|
||||
Json::Reader reader(features);
|
||||
bool parsingSuccessful = reader.parse(input, *root);
|
||||
bool parsingSuccessful = reader.parse(input.data(), input.data() + input.size(), *root);
|
||||
if (!parsingSuccessful) {
|
||||
printf("Failed to parse %s file: \n%s\n",
|
||||
kind.c_str(),
|
||||
|
@@ -98,7 +98,8 @@ Reader::Reader(const Features& features)
|
||||
|
||||
bool
|
||||
Reader::parse(const std::string& document, Value& root, bool collectComments) {
|
||||
document_ = document;
|
||||
JSONCPP_STRING documentCopy(document.data(), document.data() + document.capacity());
|
||||
std::swap(documentCopy, document_);
|
||||
const char* begin = document_.c_str();
|
||||
const char* end = begin + document_.length();
|
||||
return parse(begin, end, root, collectComments);
|
||||
@@ -114,7 +115,7 @@ bool Reader::parse(std::istream& sin, Value& root, bool collectComments) {
|
||||
// create an extra copy.
|
||||
JSONCPP_STRING doc;
|
||||
std::getline(sin, doc, (char)EOF);
|
||||
return parse(doc, root, collectComments);
|
||||
return parse(doc.data(), doc.data() + doc.size(), root, collectComments);
|
||||
}
|
||||
|
||||
bool Reader::parse(const char* beginDoc,
|
||||
|
@@ -138,7 +138,29 @@ inline static void decodePrefixedString(
|
||||
}
|
||||
/** Free the string duplicated by duplicateStringValue()/duplicateAndPrefixStringValue().
|
||||
*/
|
||||
static inline void releaseStringValue(char* value) { free(value); }
|
||||
#if JSONCPP_USING_SECURE_MEMORY
|
||||
static inline void releasePrefixedStringValue(char* value) {
|
||||
unsigned length = 0;
|
||||
char const* valueDecoded;
|
||||
decodePrefixedString(true, value, &length, &valueDecoded);
|
||||
size_t const size = sizeof(unsigned) + length + 1U;
|
||||
memset(value, 0, size);
|
||||
free(value);
|
||||
}
|
||||
static inline void releaseStringValue(char* value, unsigned length) {
|
||||
// length==0 => we allocated the strings memory
|
||||
size_t size = (length==0) ? strlen(value) : length;
|
||||
memset(value, 0, size);
|
||||
free(value);
|
||||
}
|
||||
#else // !JSONCPP_USING_SECURE_MEMORY
|
||||
static inline void releasePrefixedStringValue(char* value) {
|
||||
free(value);
|
||||
}
|
||||
static inline void releaseStringValue(char* value, unsigned length) {
|
||||
free(value);
|
||||
}
|
||||
#endif // JSONCPP_USING_SECURE_MEMORY
|
||||
|
||||
} // namespace Json
|
||||
|
||||
@@ -193,12 +215,12 @@ Value::CommentInfo::CommentInfo() : comment_(0)
|
||||
|
||||
Value::CommentInfo::~CommentInfo() {
|
||||
if (comment_)
|
||||
releaseStringValue(comment_);
|
||||
releaseStringValue(comment_, 0u);
|
||||
}
|
||||
|
||||
void Value::CommentInfo::setComment(const char* text, size_t len) {
|
||||
if (comment_) {
|
||||
releaseStringValue(comment_);
|
||||
releaseStringValue(comment_, 0u);
|
||||
comment_ = 0;
|
||||
}
|
||||
JSON_ASSERT(text != 0);
|
||||
@@ -229,10 +251,10 @@ Value::CZString::CZString(char const* str, unsigned ulength, DuplicationPolicy a
|
||||
storage_.length_ = ulength & 0x3FFFFFFF;
|
||||
}
|
||||
|
||||
Value::CZString::CZString(const CZString& other)
|
||||
: cstr_(other.storage_.policy_ != noDuplication && other.cstr_ != 0
|
||||
? duplicateStringValue(other.cstr_, other.storage_.length_)
|
||||
: other.cstr_) {
|
||||
Value::CZString::CZString(const CZString& other) {
|
||||
cstr_ = (other.storage_.policy_ != noDuplication && other.cstr_ != 0
|
||||
? duplicateStringValue(other.cstr_, other.storage_.length_)
|
||||
: other.cstr_);
|
||||
storage_.policy_ = static_cast<unsigned>(other.cstr_
|
||||
? (static_cast<DuplicationPolicy>(other.storage_.policy_) == noDuplication
|
||||
? noDuplication : duplicate)
|
||||
@@ -248,8 +270,9 @@ Value::CZString::CZString(CZString&& other)
|
||||
#endif
|
||||
|
||||
Value::CZString::~CZString() {
|
||||
if (cstr_ && storage_.policy_ == duplicate)
|
||||
releaseStringValue(const_cast<char*>(cstr_));
|
||||
if (cstr_ && storage_.policy_ == duplicate) {
|
||||
releaseStringValue(const_cast<char*>(cstr_), storage_.length_ + 1u); //+1 for null terminating character for sake of completeness but not actually necessary
|
||||
}
|
||||
}
|
||||
|
||||
void Value::CZString::swap(CZString& other) {
|
||||
@@ -455,7 +478,7 @@ Value::~Value() {
|
||||
break;
|
||||
case stringValue:
|
||||
if (allocated_)
|
||||
releaseStringValue(value_.string_);
|
||||
releasePrefixedStringValue(value_.string_);
|
||||
break;
|
||||
case arrayValue:
|
||||
case objectValue:
|
||||
@@ -467,6 +490,8 @@ Value::~Value() {
|
||||
|
||||
if (comments_)
|
||||
delete[] comments_;
|
||||
|
||||
value_.uint_ = 0;
|
||||
}
|
||||
|
||||
Value& Value::operator=(Value other) {
|
||||
@@ -611,6 +636,18 @@ const char* Value::asCString() const {
|
||||
return this_str;
|
||||
}
|
||||
|
||||
#if JSONCPP_USING_SECURE_MEMORY
|
||||
unsigned Value::getCStringLength() const {
|
||||
JSON_ASSERT_MESSAGE(type_ == stringValue,
|
||||
"in Json::Value::asCString(): requires stringValue");
|
||||
if (value_.string_ == 0) return 0;
|
||||
unsigned this_len;
|
||||
char const* this_str;
|
||||
decodePrefixedString(this->allocated_, this->value_.string_, &this_len, &this_str);
|
||||
return this_len;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool Value::getString(char const** str, char const** cend) const {
|
||||
if (type_ != stringValue) return false;
|
||||
if (value_.string_ == 0) return false;
|
||||
|
@@ -10,4 +10,11 @@
|
||||
# define JSONCPP_VERSION_QUALIFIER
|
||||
# define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8))
|
||||
|
||||
#ifdef JSONCPP_USING_SECURE_MEMORY
|
||||
#undef JSONCPP_USING_SECURE_MEMORY
|
||||
#endif
|
||||
#define JSONCPP_USING_SECURE_MEMORY @JSONCPP_USE_SECURE_MEMORY@
|
||||
// If non-zero, the library zeroes any memory that it has allocated before
|
||||
// it frees its memory.
|
||||
|
||||
#endif // JSON_VERSION_H_INCLUDED
|
||||
|
@@ -434,7 +434,7 @@ JSONCPP_STRING ToJsonString(JSONCPP_STRING in) {
|
||||
return in;
|
||||
}
|
||||
|
||||
#if JSON_USE_SECURE_MEMORY
|
||||
#if JSONCPP_USING_SECURE_MEMORY
|
||||
JSONCPP_STRING ToJsonString(std::string in) {
|
||||
return JSONCPP_STRING(in.data(), in.data() + in.length());
|
||||
}
|
||||
|
@@ -193,7 +193,7 @@ TestResult& checkEqual(TestResult& result,
|
||||
|
||||
JSONCPP_STRING ToJsonString(const char* toConvert);
|
||||
JSONCPP_STRING ToJsonString(JSONCPP_STRING in);
|
||||
#if JSON_USE_SECURE_MEMORY
|
||||
#if JSONCPP_USING_SECURE_MEMORY
|
||||
JSONCPP_STRING ToJsonString(std::string in);
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user