Skip to content

Commit ae56465

Browse files
dawesccdunn2001
dawesc
authored andcommitted
-DJSONCPP_USE_SECURE_MEMORY=1 for cmake
Add allocator.h to amalgamated header Test JSONCPP_USE_SECURE_MEMORY in Travis
1 parent f8674c6 commit ae56465

File tree

12 files changed

+81
-32
lines changed

12 files changed

+81
-32
lines changed

Diff for: CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ jsoncpp_parse_version( ${JSONCPP_VERSION} JSONCPP_VERSION )
6969
#IF(NOT JSONCPP_VERSION_FOUND)
7070
# MESSAGE(FATAL_ERROR "Failed to parse version string properly. Expect X.Y.Z")
7171
#ENDIF(NOT JSONCPP_VERSION_FOUND)
72+
SET( JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL" )
7273

7374
MESSAGE(STATUS "JsonCpp Version: ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINOR}.${JSONCPP_VERSION_PATCH}")
7475
# File version.h is only regenerated on CMake configure step

Diff for: amalgamate.py

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def amalgamate_source(source_top_dir=None,
6767
header.add_text("/// to prevent private header inclusion.")
6868
header.add_text("#define JSON_IS_AMALGAMATION")
6969
header.add_file("include/json/version.h")
70+
header.add_file("include/json/allocator.h")
7071
header.add_file("include/json/config.h")
7172
header.add_file("include/json/forwards.h")
7273
header.add_file("include/json/features.h")

Diff for: include/json/config.h

+7-11
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,9 @@
119119
# define JSON_USE_INT64_DOUBLE_CONVERSION 1
120120
#endif
121121

122-
// If non-zero, the library zeroes any memory that it has allocated before
123-
// it frees its
124-
#ifndef JSON_USE_SECURE_MEMORY
125-
#define JSON_USE_SECURE_MEMORY 0
126-
#endif
122+
#include "version.h"
127123

128-
#if JSON_USE_SECURE_MEMORY
124+
#if JSONCPP_USING_SECURE_MEMORY
129125
#include "allocator.h" //typedef Allocator
130126
#endif
131127

@@ -149,19 +145,19 @@ typedef Int64 LargestInt;
149145
typedef UInt64 LargestUInt;
150146
#define JSON_HAS_INT64
151147
#endif // if defined(JSON_NO_INT64)
152-
#if JSON_USE_SECURE_MEMORY
153-
#define JSONCPP_STRING std::basic_string<char, std::char_traits<char>, SecureAllocator<char> >
154-
#define JSONCPP_OSTRINGSTREAM std::basic_ostringstream<char, std::char_traits<char>, SecureAllocator<char> >
148+
#if JSONCPP_USING_SECURE_MEMORY
149+
#define JSONCPP_STRING std::basic_string<char, std::char_traits<char>, Json::SecureAllocator<char> >
150+
#define JSONCPP_OSTRINGSTREAM std::basic_ostringstream<char, std::char_traits<char>, Json::SecureAllocator<char> >
155151
#define JSONCPP_OSTREAM std::basic_ostream<char, std::char_traits<char>>
156-
#define JSONCPP_ISTRINGSTREAM std::basic_istringstream<char, std::char_traits<char>, SecureAllocator<char> >
152+
#define JSONCPP_ISTRINGSTREAM std::basic_istringstream<char, std::char_traits<char>, Json::SecureAllocator<char> >
157153
#define JSONCPP_ISTREAM std::istream
158154
#else
159155
#define JSONCPP_STRING std::string
160156
#define JSONCPP_OSTRINGSTREAM std::ostringstream
161157
#define JSONCPP_OSTREAM std::ostream
162158
#define JSONCPP_ISTRINGSTREAM std::istringstream
163159
#define JSONCPP_ISTREAM std::istream
164-
#endif // if JSON_USE_SECURE_MEMORY
160+
#endif // if JSONCPP_USING_SECURE_MEMORY
165161
} // end namespace Json
166162

167163
#endif // JSON_CONFIG_H_INCLUDED

Diff for: include/json/value.h

+3
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ Json::Value obj_value(Json::objectValue); // {}
336336
int compare(const Value& other) const;
337337

338338
const char* asCString() const; ///< Embedded zeroes could cause you trouble!
339+
#if JSONCPP_USING_SECURE_MEMORY
340+
unsigned getCStringLength() const; //Allows you to understand the length of the CString
341+
#endif
339342
JSONCPP_STRING asString() const; ///< Embedded zeroes are possible.
340343
/** Get raw char* of string-value.
341344
* \return false if !string. (Seg-fault if str or end are NULL.)

Diff for: include/json/version.h

+7
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@
1010
# define JSONCPP_VERSION_QUALIFIER
1111
# define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8))
1212

13+
#ifdef JSONCPP_USING_SECURE_MEMORY
14+
#undef JSONCPP_USING_SECURE_MEMORY
15+
#endif
16+
#define JSONCPP_USING_SECURE_MEMORY 1
17+
// If non-zero, the library zeroes any memory that it has allocated before
18+
// it frees its memory.
19+
1320
#endif // JSON_VERSION_H_INCLUDED

Diff for: src/jsontestrunner/main.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ static JSONCPP_STRING normalizeFloatingPointStr(double value) {
3333
#endif
3434
buffer[sizeof(buffer) - 1] = 0;
3535
JSONCPP_STRING s(buffer);
36-
#if JSON_USE_SECURE_MEMORY
37-
memset(&buffer, 0, sizeof(buffer));
38-
#endif
3936
JSONCPP_STRING::size_type index = s.find_last_of("eE");
4037
if (index != JSONCPP_STRING::npos) {
4138
JSONCPP_STRING::size_type hasSign =
@@ -69,9 +66,6 @@ static JSONCPP_STRING readInputTestFile(const char* path) {
6966
if (fread(buffer, 1, usize, file) == usize)
7067
text = buffer;
7168
fclose(file);
72-
#if JSON_USE_SECURE_MEMORY
73-
memset(buffer, 0, static_cast<size_t>(size + 1));
74-
#endif
7569
delete[] buffer;
7670
return text;
7771
}
@@ -151,7 +145,7 @@ static int parseAndSaveValueTree(const JSONCPP_STRING& input,
151145
Json::Value* root)
152146
{
153147
Json::Reader reader(features);
154-
bool parsingSuccessful = reader.parse(input, *root);
148+
bool parsingSuccessful = reader.parse(input.data(), input.data() + input.size(), *root);
155149
if (!parsingSuccessful) {
156150
printf("Failed to parse %s file: \n%s\n",
157151
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

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

143165
} // namespace Json
144166

@@ -193,12 +215,12 @@ Value::CommentInfo::CommentInfo() : comment_(0)
193215

194216
Value::CommentInfo::~CommentInfo() {
195217
if (comment_)
196-
releaseStringValue(comment_);
218+
releaseStringValue(comment_, 0u);
197219
}
198220

199221
void Value::CommentInfo::setComment(const char* text, size_t len) {
200222
if (comment_) {
201-
releaseStringValue(comment_);
223+
releaseStringValue(comment_, 0u);
202224
comment_ = 0;
203225
}
204226
JSON_ASSERT(text != 0);
@@ -229,10 +251,10 @@ Value::CZString::CZString(char const* str, unsigned ulength, DuplicationPolicy a
229251
storage_.length_ = ulength & 0x3FFFFFFF;
230252
}
231253

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_);
236258
storage_.policy_ = static_cast<unsigned>(other.cstr_
237259
? (static_cast<DuplicationPolicy>(other.storage_.policy_) == noDuplication
238260
? noDuplication : duplicate)
@@ -248,8 +270,9 @@ Value::CZString::CZString(CZString&& other)
248270
#endif
249271

250272
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+
}
253276
}
254277

255278
void Value::CZString::swap(CZString& other) {
@@ -455,7 +478,7 @@ Value::~Value() {
455478
break;
456479
case stringValue:
457480
if (allocated_)
458-
releaseStringValue(value_.string_);
481+
releasePrefixedStringValue(value_.string_);
459482
break;
460483
case arrayValue:
461484
case objectValue:
@@ -467,6 +490,8 @@ Value::~Value() {
467490

468491
if (comments_)
469492
delete[] comments_;
493+
494+
value_.uint_ = 0;
470495
}
471496

472497
Value& Value::operator=(Value other) {
@@ -611,6 +636,18 @@ const char* Value::asCString() const {
611636
return this_str;
612637
}
613638

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

Diff for: src/lib_json/version.h.in

+7
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@
1010
# define JSONCPP_VERSION_QUALIFIER
1111
# define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8))
1212

13+
#ifdef JSONCPP_USING_SECURE_MEMORY
14+
#undef JSONCPP_USING_SECURE_MEMORY
15+
#endif
16+
#define JSONCPP_USING_SECURE_MEMORY @JSONCPP_USE_SECURE_MEMORY@
17+
// If non-zero, the library zeroes any memory that it has allocated before
18+
// it frees its memory.
19+
1320
#endif // JSON_VERSION_H_INCLUDED

Diff for: src/test_lib_json/jsontest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ JSONCPP_STRING ToJsonString(JSONCPP_STRING in) {
434434
return in;
435435
}
436436

437-
#if JSON_USE_SECURE_MEMORY
437+
#if JSONCPP_USING_SECURE_MEMORY
438438
JSONCPP_STRING ToJsonString(std::string in) {
439439
return JSONCPP_STRING(in.data(), in.data() + in.length());
440440
}

Diff for: src/test_lib_json/jsontest.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ TestResult& checkEqual(TestResult& result,
193193

194194
JSONCPP_STRING ToJsonString(const char* toConvert);
195195
JSONCPP_STRING ToJsonString(JSONCPP_STRING in);
196-
#if JSON_USE_SECURE_MEMORY
196+
#if JSONCPP_USING_SECURE_MEMORY
197197
JSONCPP_STRING ToJsonString(std::string in);
198198
#endif
199199

Diff for: travis.sh

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ env | sort
1919

2020
cmake -DJSONCPP_WITH_CMAKE_PACKAGE=$CMAKE_PKG -DBUILD_SHARED_LIBS=$SHARED_LIB -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_VERBOSE_MAKEFILE=$VERBOSE_MAKE .
2121
make
22+
cmake -DJSONCPP_WITH_CMAKE_PACKAGE=$CMAKE_PKG -DBUILD_SHARED_LIBS=$SHARED_LIB -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_VERBOSE_MAKEFILE=$VERBOSE_MAKE -DJSONCPP_USE_SECURE_MEMORY=1 .
23+
make
2224

2325
# Python is not available in Travis for osx.
2426
# https://github.com/travis-ci/travis-ci/issues/2320

0 commit comments

Comments
 (0)