Skip to content

Multiple fixes for issues found by Cppcheck #784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,12 @@ Json::Value obj_value(Json::objectValue); // {}
/// \post type() is unchanged
void clear();

/// Resize the array to size elements.
/// Resize the array to newSize elements.
/// New elements are initialized to null.
/// May only be called on nullValue or arrayValue.
/// \pre type() is arrayValue or nullValue
/// \post type() is arrayValue
void resize(ArrayIndex size);
void resize(ArrayIndex newSize);

/// Access an array element (zero based index ).
/// If the array contains less than index element, then null value are
Expand Down Expand Up @@ -562,10 +562,10 @@ Json::Value obj_value(Json::objectValue); // {}
/** \brief Remove the indexed array element.

O(n) expensive operations.
Update 'removed' iff removed.
\return true iff removed (no exceptions)
Update 'removed' if removed.
\return true if removed (no exceptions)
*/
bool removeIndex(ArrayIndex i, Value* removed);
bool removeIndex(ArrayIndex index, Value* removed);

/// Return true if the object has a member named key.
/// \note 'key' must be null-terminated.
Expand Down Expand Up @@ -720,7 +720,7 @@ class JSON_API Path {
const InArgs& in,
InArgs::const_iterator& itInArg,
PathArgument::Kind kind);
void invalidPath(const JSONCPP_STRING& path, int location);
static void invalidPath(const JSONCPP_STRING& path, int location);

Args args_;
};
Expand Down
6 changes: 3 additions & 3 deletions include/json/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
void unindent();
void writeCommentBeforeValue(const Value& root);
void writeCommentAfterValueOnSameLine(const Value& root);
bool hasCommentForValue(const Value& value);
static bool hasCommentForValue(const Value& value);
static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);

typedef std::vector<JSONCPP_STRING> ChildValues;
Expand Down Expand Up @@ -300,7 +300,7 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
/**
* \param indentation Each level will be indented by this amount extra.
*/
StyledStreamWriter(JSONCPP_STRING indentation = "\t");
StyledStreamWriter(const JSONCPP_STRING& indentation = "\t");
~StyledStreamWriter() {}

public:
Expand All @@ -323,7 +323,7 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
void unindent();
void writeCommentBeforeValue(const Value& root);
void writeCommentAfterValueOnSameLine(const Value& root);
bool hasCommentForValue(const Value& value);
static bool hasCommentForValue(const Value& value);
static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);

typedef std::vector<JSONCPP_STRING> ChildValues;
Expand Down
4 changes: 2 additions & 2 deletions src/jsontestrunner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ static void printValueTree(FILE* fout,
for (Json::ArrayIndex index = 0; index < size; ++index) {
static char buffer[16];
#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
sprintf_s(buffer, sizeof(buffer), "[%d]", index);
sprintf_s(buffer, sizeof(buffer), "[%u]", index);
#else
snprintf(buffer, sizeof(buffer), "[%d]", index);
snprintf(buffer, sizeof(buffer), "[%u]", index);
#endif
printValueTree(fout, value[index], path + buffer);
}
Expand Down
58 changes: 29 additions & 29 deletions src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,16 @@ bool Reader::parse(const std::string& document,
return parse(begin, end, root, collectComments);
}

bool Reader::parse(std::istream& sin, Value& root, bool collectComments) {
// std::istream_iterator<char> begin(sin);
bool Reader::parse(std::istream& is, Value& root, bool collectComments) {
// std::istream_iterator<char> begin(is);
// std::istream_iterator<char> end;
// Those would allow streamed input from a file, if parse() were a
// template function.

// Since JSONCPP_STRING is reference-counted, this at least does not
// create an extra copy.
JSONCPP_STRING doc;
std::getline(sin, doc, (char)EOF);
std::getline(is, doc, (char)EOF);
return parse(doc.data(), doc.data() + doc.size(), root, collectComments);
}

Expand Down Expand Up @@ -460,12 +460,12 @@ bool Reader::readString() {
return c == '"';
}

bool Reader::readObject(Token& tokenStart) {
bool Reader::readObject(Token& token) {
Token tokenName;
JSONCPP_STRING name;
Value init(objectValue);
currentValue().swapPayload(init);
currentValue().setOffsetStart(tokenStart.start_ - begin_);
currentValue().setOffsetStart(token.start_ - begin_);
while (readToken(tokenName)) {
bool initialTokenOk = true;
while (tokenName.type_ == tokenComment && initialTokenOk)
Expand Down Expand Up @@ -516,10 +516,10 @@ bool Reader::readObject(Token& tokenStart) {
tokenObjectEnd);
}

bool Reader::readArray(Token& tokenStart) {
bool Reader::readArray(Token& token) {
Value init(arrayValue);
currentValue().swapPayload(init);
currentValue().setOffsetStart(tokenStart.start_ - begin_);
currentValue().setOffsetStart(token.start_ - begin_);
skipSpaces();
if (current_ != end_ && *current_ == ']') // empty array
{
Expand All @@ -536,19 +536,19 @@ bool Reader::readArray(Token& tokenStart) {
if (!ok) // error already set
return recoverFromError(tokenArrayEnd);

Token token;
Token currentToken;
// Accept Comment after last item in the array.
ok = readToken(token);
while (token.type_ == tokenComment && ok) {
ok = readToken(token);
ok = readToken(currentToken);
while (currentToken.type_ == tokenComment && ok) {
ok = readToken(currentToken);
}
bool badTokenType =
(token.type_ != tokenArraySeparator && token.type_ != tokenArrayEnd);
bool badTokenType = (currentToken.type_ != tokenArraySeparator &&
currentToken.type_ != tokenArrayEnd);
if (!ok || badTokenType) {
return addErrorAndRecover("Missing ',' or ']' in array declaration",
token, tokenArrayEnd);
currentToken, tokenArrayEnd);
}
if (token.type_ == tokenArrayEnd)
if (currentToken.type_ == tokenArrayEnd)
break;
}
return true;
Expand Down Expand Up @@ -706,8 +706,8 @@ bool Reader::decodeUnicodeCodePoint(Token& token,
return addError(
"additional six characters expected to parse unicode surrogate pair.",
token, current);
unsigned int surrogatePair;
if (*(current++) == '\\' && *(current++) == 'u') {
unsigned int surrogatePair;
if (decodeUnicodeEscapeSequence(token, current, end, surrogatePair)) {
unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
} else
Expand Down Expand Up @@ -1450,12 +1450,12 @@ bool OurReader::readStringSingleQuote() {
return c == '\'';
}

bool OurReader::readObject(Token& tokenStart) {
bool OurReader::readObject(Token& token) {
Token tokenName;
JSONCPP_STRING name;
Value init(objectValue);
currentValue().swapPayload(init);
currentValue().setOffsetStart(tokenStart.start_ - begin_);
currentValue().setOffsetStart(token.start_ - begin_);
while (readToken(tokenName)) {
bool initialTokenOk = true;
while (tokenName.type_ == tokenComment && initialTokenOk)
Expand Down Expand Up @@ -1512,10 +1512,10 @@ bool OurReader::readObject(Token& tokenStart) {
tokenObjectEnd);
}

bool OurReader::readArray(Token& tokenStart) {
bool OurReader::readArray(Token& token) {
Value init(arrayValue);
currentValue().swapPayload(init);
currentValue().setOffsetStart(tokenStart.start_ - begin_);
currentValue().setOffsetStart(token.start_ - begin_);
skipSpaces();
if (current_ != end_ && *current_ == ']') // empty array
{
Expand All @@ -1532,19 +1532,19 @@ bool OurReader::readArray(Token& tokenStart) {
if (!ok) // error already set
return recoverFromError(tokenArrayEnd);

Token token;
Token currentToken;
// Accept Comment after last item in the array.
ok = readToken(token);
while (token.type_ == tokenComment && ok) {
ok = readToken(token);
ok = readToken(currentToken);
while (currentToken.type_ == tokenComment && ok) {
ok = readToken(currentToken);
}
bool badTokenType =
(token.type_ != tokenArraySeparator && token.type_ != tokenArrayEnd);
bool badTokenType = (currentToken.type_ != tokenArraySeparator &&
currentToken.type_ != tokenArrayEnd);
if (!ok || badTokenType) {
return addErrorAndRecover("Missing ',' or ']' in array declaration",
token, tokenArrayEnd);
currentToken, tokenArrayEnd);
}
if (token.type_ == tokenArrayEnd)
if (currentToken.type_ == tokenArrayEnd)
break;
}
return true;
Expand Down Expand Up @@ -1726,8 +1726,8 @@ bool OurReader::decodeUnicodeCodePoint(Token& token,
return addError(
"additional six characters expected to parse unicode surrogate pair.",
token, current);
unsigned int surrogatePair;
if (*(current++) == '\\' && *(current++) == 'u') {
unsigned int surrogatePair;
if (decodeUnicodeEscapeSequence(token, current, end, surrogatePair)) {
unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
} else
Expand Down
50 changes: 25 additions & 25 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ void Value::CommentInfo::setComment(const char* text, size_t len) {
// Notes: policy_ indicates if the string was allocated when
// a string is stored.

Value::CZString::CZString(ArrayIndex aindex) : cstr_(0), index_(aindex) {}
Value::CZString::CZString(ArrayIndex index) : cstr_(0), index_(index) {}

Value::CZString::CZString(char const* str,
unsigned ulength,
unsigned length,
DuplicationPolicy allocate)
: cstr_(str) {
// allocate != duplicate
storage_.policy_ = allocate & 0x3;
storage_.length_ = ulength & 0x3FFFFFFF;
storage_.length_ = length & 0x3FFFFFFF;
}

Value::CZString::CZString(const CZString& other) {
Expand Down Expand Up @@ -357,10 +357,10 @@ bool Value::CZString::isStaticString() const {
* memset( this, 0, sizeof(Value) )
* This optimization is used in ValueInternalMap fast allocator.
*/
Value::Value(ValueType vtype) {
Value::Value(ValueType type) {
static char const emptyString[] = "";
initBasic(vtype);
switch (vtype) {
initBasic(type);
switch (type) {
case nullValue:
break;
case intValue:
Expand Down Expand Up @@ -418,10 +418,10 @@ Value::Value(const char* value) {
value, static_cast<unsigned>(strlen(value)));
}

Value::Value(const char* beginValue, const char* endValue) {
Value::Value(const char* begin, const char* end) {
initBasic(stringValue, true);
value_.string_ = duplicateAndPrefixStringValue(
beginValue, static_cast<unsigned>(endValue - beginValue));
value_.string_ =
duplicateAndPrefixStringValue(begin, static_cast<unsigned>(end - begin));
}

Value::Value(const JSONCPP_STRING& value) {
Expand Down Expand Up @@ -645,14 +645,14 @@ unsigned Value::getCStringLength() const {
}
#endif

bool Value::getString(char const** str, char const** cend) const {
bool Value::getString(char const** begin, char const** end) const {
if (type_ != stringValue)
return false;
if (value_.string_ == 0)
return false;
unsigned length;
decodePrefixedString(this->allocated_, this->value_.string_, &length, str);
*cend = *str + length;
decodePrefixedString(this->allocated_, this->value_.string_, &length, begin);
*end = *begin + length;
return true;
}

Expand Down Expand Up @@ -1003,8 +1003,8 @@ const Value& Value::operator[](int index) const {
return (*this)[ArrayIndex(index)];
}

void Value::initBasic(ValueType vtype, bool allocated) {
type_ = vtype;
void Value::initBasic(ValueType type, bool allocated) {
type_ = type;
allocated_ = allocated;
comments_ = 0;
start_ = 0;
Expand Down Expand Up @@ -1101,13 +1101,13 @@ Value& Value::resolveReference(const char* key) {
}

// @param key is not null-terminated.
Value& Value::resolveReference(char const* key, char const* cend) {
Value& Value::resolveReference(char const* key, char const* end) {
JSON_ASSERT_MESSAGE(
type_ == nullValue || type_ == objectValue,
"in Json::Value::resolveReference(key, end): requires objectValue");
if (type_ == nullValue)
*this = Value(objectValue);
CZString actualKey(key, static_cast<unsigned>(cend - key),
CZString actualKey(key, static_cast<unsigned>(end - key),
CZString::duplicateOnCopy);
ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
if (it != value_.map_->end() && (*it).first == actualKey)
Expand All @@ -1126,13 +1126,13 @@ Value Value::get(ArrayIndex index, const Value& defaultValue) const {

bool Value::isValidIndex(ArrayIndex index) const { return index < size(); }

Value const* Value::find(char const* key, char const* cend) const {
Value const* Value::find(char const* begin, char const* end) const {
JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == objectValue,
"in Json::Value::find(key, end, found): requires "
"objectValue or nullValue");
if (type_ == nullValue)
return NULL;
CZString actualKey(key, static_cast<unsigned>(cend - key),
CZString actualKey(begin, static_cast<unsigned>(end - begin),
CZString::noDuplication);
ObjectValues::const_iterator it = value_.map_->find(actualKey);
if (it == value_.map_->end())
Expand Down Expand Up @@ -1184,10 +1184,10 @@ Value& Value::append(Value&& value) {
}
#endif

Value Value::get(char const* key,
char const* cend,
Value Value::get(char const* begin,
char const* end,
Value const& defaultValue) const {
Value const* found = find(key, cend);
Value const* found = find(begin, end);
return !found ? defaultValue : *found;
}
Value Value::get(char const* key, Value const& defaultValue) const {
Expand All @@ -1197,11 +1197,11 @@ Value Value::get(JSONCPP_STRING const& key, Value const& defaultValue) const {
return get(key.data(), key.data() + key.length(), defaultValue);
}

bool Value::removeMember(const char* key, const char* cend, Value* removed) {
bool Value::removeMember(const char* begin, const char* end, Value* removed) {
if (type_ != objectValue) {
return false;
}
CZString actualKey(key, static_cast<unsigned>(cend - key),
CZString actualKey(begin, static_cast<unsigned>(end - begin),
CZString::noDuplication);
ObjectValues::iterator it = value_.map_->find(actualKey);
if (it == value_.map_->end())
Expand Down Expand Up @@ -1264,8 +1264,8 @@ Value Value::get(const CppTL::ConstString& key,
}
#endif

bool Value::isMember(char const* key, char const* cend) const {
Value const* value = find(key, cend);
bool Value::isMember(char const* begin, char const* end) const {
Value const* value = find(begin, end);
return NULL != value;
}
bool Value::isMember(char const* key) const {
Expand Down
6 changes: 3 additions & 3 deletions src/lib_json/json_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ bool StyledWriter::hasCommentForValue(const Value& value) {
// Class StyledStreamWriter
// //////////////////////////////////////////////////////////////////

StyledStreamWriter::StyledStreamWriter(JSONCPP_STRING indentation)
StyledStreamWriter::StyledStreamWriter(const JSONCPP_STRING& indentation)
: document_(NULL), rightMargin_(74), indentation_(indentation),
addChildValues_(), indented_(false) {}

Expand Down Expand Up @@ -1245,10 +1245,10 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings) {
//! [StreamWriterBuilderDefaults]
}

JSONCPP_STRING writeString(StreamWriter::Factory const& builder,
JSONCPP_STRING writeString(StreamWriter::Factory const& factory,
Value const& root) {
JSONCPP_OSTRINGSTREAM sout;
StreamWriterPtr const writer(builder.newStreamWriter());
StreamWriterPtr const writer(factory.newStreamWriter());
writer->write(root, &sout);
return sout.str();
}
Expand Down
Loading