Skip to content

Commit

Permalink
Merge pull request #732 from miloyip/issue731_writerstringassert
Browse files Browse the repository at this point in the history
Add preconditions in writer and string functions
  • Loading branch information
miloyip committed Sep 3, 2016
2 parents 9f66882 + 3e2172b commit 328ead0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
7 changes: 5 additions & 2 deletions example/parsebyparts/parsebyparts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ class AsyncDocumentParser {
AsyncDocumentParser(Document& d)
: stream_(*this)
, d_(d)
, parseThread_(&AsyncDocumentParser::Parse, this)
, parseThread_()
, mutex_()
, notEmpty_()
, finish_()
, completed_()
{}
{
// Create and execute thread after all member variables are initialized.
parseThread_ = std::thread(&AsyncDocumentParser::Parse, this);
}

~AsyncDocumentParser() {
if (!parseThread_.joinable())
Expand Down
3 changes: 3 additions & 0 deletions include/rapidjson/internal/strfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace internal {
*/
template <typename Ch>
inline SizeType StrLen(const Ch* s) {
RAPIDJSON_ASSERT(s != 0);
const Ch* p = s;
while (*p) ++p;
return SizeType(p - s);
Expand All @@ -36,6 +37,8 @@ inline SizeType StrLen(const Ch* s) {
//! Returns number of code points in a encoded string.
template<typename Encoding>
bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
RAPIDJSON_ASSERT(s != 0);
RAPIDJSON_ASSERT(outCount != 0);
GenericStringStream<Encoding> is(s);
const typename Encoding::Ch* end = s + length;
SizeType count = 0;
Expand Down
8 changes: 7 additions & 1 deletion include/rapidjson/prettywriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,14 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); }

bool RawNumber(const Ch* str, SizeType length, bool copy = false) {
RAPIDJSON_ASSERT(str != 0);
(void)copy;
PrettyPrefix(kNumberType);
return Base::WriteString(str, length);
}

bool String(const Ch* str, SizeType length, bool copy = false) {
RAPIDJSON_ASSERT(str != 0);
(void)copy;
PrettyPrefix(kStringType);
return Base::WriteString(str, length);
Expand Down Expand Up @@ -184,7 +186,11 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
\param type Type of the root of json.
\note When using PrettyWriter::RawValue(), the result json may not be indented correctly.
*/
bool RawValue(const Ch* json, size_t length, Type type) { PrettyPrefix(type); return Base::WriteRawValue(json, length); }
bool RawValue(const Ch* json, size_t length, Type type) {
RAPIDJSON_ASSERT(json != 0);
PrettyPrefix(type);
return Base::WriteRawValue(json, length);
}

protected:
void PrettyPrefix(Type type) {
Expand Down
8 changes: 7 additions & 1 deletion include/rapidjson/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,14 @@ class Writer {
bool Double(double d) { Prefix(kNumberType); return EndValue(WriteDouble(d)); }

bool RawNumber(const Ch* str, SizeType length, bool copy = false) {
RAPIDJSON_ASSERT(str != 0);
(void)copy;
Prefix(kNumberType);
return EndValue(WriteString(str, length));
}

bool String(const Ch* str, SizeType length, bool copy = false) {
RAPIDJSON_ASSERT(str != 0);
(void)copy;
Prefix(kStringType);
return EndValue(WriteString(str, length));
Expand Down Expand Up @@ -249,7 +251,11 @@ class Writer {
\param length Length of the json.
\param type Type of the root of json.
*/
bool RawValue(const Ch* json, size_t length, Type type) { Prefix(type); return EndValue(WriteRawValue(json, length)); }
bool RawValue(const Ch* json, size_t length, Type type) {
RAPIDJSON_ASSERT(json != 0);
Prefix(type);
return EndValue(WriteRawValue(json, length));
}

protected:
//! Information for each nested level
Expand Down

0 comments on commit 328ead0

Please sign in to comment.