Skip to content

Commit c4103ab

Browse files
authored
Merge pull request open-source-parsers#784 from Nekto89/cppcheck_fix
Multiple fixes for issues found by Cppcheck
2 parents cfab607 + a5d7c71 commit c4103ab

File tree

8 files changed

+72
-83
lines changed

8 files changed

+72
-83
lines changed

include/json/value.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,12 @@ Json::Value obj_value(Json::objectValue); // {}
427427
/// \post type() is unchanged
428428
void clear();
429429

430-
/// Resize the array to size elements.
430+
/// Resize the array to newSize elements.
431431
/// New elements are initialized to null.
432432
/// May only be called on nullValue or arrayValue.
433433
/// \pre type() is arrayValue or nullValue
434434
/// \post type() is arrayValue
435-
void resize(ArrayIndex size);
435+
void resize(ArrayIndex newSize);
436436

437437
/// Access an array element (zero based index ).
438438
/// If the array contains less than index element, then null value are
@@ -562,10 +562,10 @@ Json::Value obj_value(Json::objectValue); // {}
562562
/** \brief Remove the indexed array element.
563563
564564
O(n) expensive operations.
565-
Update 'removed' iff removed.
566-
\return true iff removed (no exceptions)
565+
Update 'removed' if removed.
566+
\return true if removed (no exceptions)
567567
*/
568-
bool removeIndex(ArrayIndex i, Value* removed);
568+
bool removeIndex(ArrayIndex index, Value* removed);
569569

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

725725
Args args_;
726726
};

include/json/writer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
249249
void unindent();
250250
void writeCommentBeforeValue(const Value& root);
251251
void writeCommentAfterValueOnSameLine(const Value& root);
252-
bool hasCommentForValue(const Value& value);
252+
static bool hasCommentForValue(const Value& value);
253253
static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
254254

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

306306
public:
@@ -323,7 +323,7 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
323323
void unindent();
324324
void writeCommentBeforeValue(const Value& root);
325325
void writeCommentAfterValueOnSameLine(const Value& root);
326-
bool hasCommentForValue(const Value& value);
326+
static bool hasCommentForValue(const Value& value);
327327
static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
328328

329329
typedef std::vector<JSONCPP_STRING> ChildValues;

src/jsontestrunner/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ static void printValueTree(FILE* fout,
106106
for (Json::ArrayIndex index = 0; index < size; ++index) {
107107
static char buffer[16];
108108
#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
109-
sprintf_s(buffer, sizeof(buffer), "[%d]", index);
109+
sprintf_s(buffer, sizeof(buffer), "[%u]", index);
110110
#else
111-
snprintf(buffer, sizeof(buffer), "[%d]", index);
111+
snprintf(buffer, sizeof(buffer), "[%u]", index);
112112
#endif
113113
printValueTree(fout, value[index], path + buffer);
114114
}

src/lib_json/json_reader.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,16 @@ bool Reader::parse(const std::string& document,
112112
return parse(begin, end, root, collectComments);
113113
}
114114

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

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

@@ -460,12 +460,12 @@ bool Reader::readString() {
460460
return c == '"';
461461
}
462462

463-
bool Reader::readObject(Token& tokenStart) {
463+
bool Reader::readObject(Token& token) {
464464
Token tokenName;
465465
JSONCPP_STRING name;
466466
Value init(objectValue);
467467
currentValue().swapPayload(init);
468-
currentValue().setOffsetStart(tokenStart.start_ - begin_);
468+
currentValue().setOffsetStart(token.start_ - begin_);
469469
while (readToken(tokenName)) {
470470
bool initialTokenOk = true;
471471
while (tokenName.type_ == tokenComment && initialTokenOk)
@@ -516,10 +516,10 @@ bool Reader::readObject(Token& tokenStart) {
516516
tokenObjectEnd);
517517
}
518518

519-
bool Reader::readArray(Token& tokenStart) {
519+
bool Reader::readArray(Token& token) {
520520
Value init(arrayValue);
521521
currentValue().swapPayload(init);
522-
currentValue().setOffsetStart(tokenStart.start_ - begin_);
522+
currentValue().setOffsetStart(token.start_ - begin_);
523523
skipSpaces();
524524
if (current_ != end_ && *current_ == ']') // empty array
525525
{
@@ -536,19 +536,19 @@ bool Reader::readArray(Token& tokenStart) {
536536
if (!ok) // error already set
537537
return recoverFromError(tokenArrayEnd);
538538

539-
Token token;
539+
Token currentToken;
540540
// Accept Comment after last item in the array.
541-
ok = readToken(token);
542-
while (token.type_ == tokenComment && ok) {
543-
ok = readToken(token);
541+
ok = readToken(currentToken);
542+
while (currentToken.type_ == tokenComment && ok) {
543+
ok = readToken(currentToken);
544544
}
545-
bool badTokenType =
546-
(token.type_ != tokenArraySeparator && token.type_ != tokenArrayEnd);
545+
bool badTokenType = (currentToken.type_ != tokenArraySeparator &&
546+
currentToken.type_ != tokenArrayEnd);
547547
if (!ok || badTokenType) {
548548
return addErrorAndRecover("Missing ',' or ']' in array declaration",
549-
token, tokenArrayEnd);
549+
currentToken, tokenArrayEnd);
550550
}
551-
if (token.type_ == tokenArrayEnd)
551+
if (currentToken.type_ == tokenArrayEnd)
552552
break;
553553
}
554554
return true;
@@ -706,8 +706,8 @@ bool Reader::decodeUnicodeCodePoint(Token& token,
706706
return addError(
707707
"additional six characters expected to parse unicode surrogate pair.",
708708
token, current);
709-
unsigned int surrogatePair;
710709
if (*(current++) == '\\' && *(current++) == 'u') {
710+
unsigned int surrogatePair;
711711
if (decodeUnicodeEscapeSequence(token, current, end, surrogatePair)) {
712712
unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
713713
} else
@@ -1450,12 +1450,12 @@ bool OurReader::readStringSingleQuote() {
14501450
return c == '\'';
14511451
}
14521452

1453-
bool OurReader::readObject(Token& tokenStart) {
1453+
bool OurReader::readObject(Token& token) {
14541454
Token tokenName;
14551455
JSONCPP_STRING name;
14561456
Value init(objectValue);
14571457
currentValue().swapPayload(init);
1458-
currentValue().setOffsetStart(tokenStart.start_ - begin_);
1458+
currentValue().setOffsetStart(token.start_ - begin_);
14591459
while (readToken(tokenName)) {
14601460
bool initialTokenOk = true;
14611461
while (tokenName.type_ == tokenComment && initialTokenOk)
@@ -1512,10 +1512,10 @@ bool OurReader::readObject(Token& tokenStart) {
15121512
tokenObjectEnd);
15131513
}
15141514

1515-
bool OurReader::readArray(Token& tokenStart) {
1515+
bool OurReader::readArray(Token& token) {
15161516
Value init(arrayValue);
15171517
currentValue().swapPayload(init);
1518-
currentValue().setOffsetStart(tokenStart.start_ - begin_);
1518+
currentValue().setOffsetStart(token.start_ - begin_);
15191519
skipSpaces();
15201520
if (current_ != end_ && *current_ == ']') // empty array
15211521
{
@@ -1532,19 +1532,19 @@ bool OurReader::readArray(Token& tokenStart) {
15321532
if (!ok) // error already set
15331533
return recoverFromError(tokenArrayEnd);
15341534

1535-
Token token;
1535+
Token currentToken;
15361536
// Accept Comment after last item in the array.
1537-
ok = readToken(token);
1538-
while (token.type_ == tokenComment && ok) {
1539-
ok = readToken(token);
1537+
ok = readToken(currentToken);
1538+
while (currentToken.type_ == tokenComment && ok) {
1539+
ok = readToken(currentToken);
15401540
}
1541-
bool badTokenType =
1542-
(token.type_ != tokenArraySeparator && token.type_ != tokenArrayEnd);
1541+
bool badTokenType = (currentToken.type_ != tokenArraySeparator &&
1542+
currentToken.type_ != tokenArrayEnd);
15431543
if (!ok || badTokenType) {
15441544
return addErrorAndRecover("Missing ',' or ']' in array declaration",
1545-
token, tokenArrayEnd);
1545+
currentToken, tokenArrayEnd);
15461546
}
1547-
if (token.type_ == tokenArrayEnd)
1547+
if (currentToken.type_ == tokenArrayEnd)
15481548
break;
15491549
}
15501550
return true;
@@ -1726,8 +1726,8 @@ bool OurReader::decodeUnicodeCodePoint(Token& token,
17261726
return addError(
17271727
"additional six characters expected to parse unicode surrogate pair.",
17281728
token, current);
1729-
unsigned int surrogatePair;
17301729
if (*(current++) == '\\' && *(current++) == 'u') {
1730+
unsigned int surrogatePair;
17311731
if (decodeUnicodeEscapeSequence(token, current, end, surrogatePair)) {
17321732
unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
17331733
} else

src/lib_json/json_value.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,15 @@ void Value::CommentInfo::setComment(const char* text, size_t len) {
241241
// Notes: policy_ indicates if the string was allocated when
242242
// a string is stored.
243243

244-
Value::CZString::CZString(ArrayIndex aindex) : cstr_(0), index_(aindex) {}
244+
Value::CZString::CZString(ArrayIndex index) : cstr_(0), index_(index) {}
245245

246246
Value::CZString::CZString(char const* str,
247-
unsigned ulength,
247+
unsigned length,
248248
DuplicationPolicy allocate)
249249
: cstr_(str) {
250250
// allocate != duplicate
251251
storage_.policy_ = allocate & 0x3;
252-
storage_.length_ = ulength & 0x3FFFFFFF;
252+
storage_.length_ = length & 0x3FFFFFFF;
253253
}
254254

255255
Value::CZString::CZString(const CZString& other) {
@@ -357,10 +357,10 @@ bool Value::CZString::isStaticString() const {
357357
* memset( this, 0, sizeof(Value) )
358358
* This optimization is used in ValueInternalMap fast allocator.
359359
*/
360-
Value::Value(ValueType vtype) {
360+
Value::Value(ValueType type) {
361361
static char const emptyString[] = "";
362-
initBasic(vtype);
363-
switch (vtype) {
362+
initBasic(type);
363+
switch (type) {
364364
case nullValue:
365365
break;
366366
case intValue:
@@ -418,10 +418,10 @@ Value::Value(const char* value) {
418418
value, static_cast<unsigned>(strlen(value)));
419419
}
420420

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

427427
Value::Value(const JSONCPP_STRING& value) {
@@ -645,14 +645,14 @@ unsigned Value::getCStringLength() const {
645645
}
646646
#endif
647647

648-
bool Value::getString(char const** str, char const** cend) const {
648+
bool Value::getString(char const** begin, char const** end) const {
649649
if (type_ != stringValue)
650650
return false;
651651
if (value_.string_ == 0)
652652
return false;
653653
unsigned length;
654-
decodePrefixedString(this->allocated_, this->value_.string_, &length, str);
655-
*cend = *str + length;
654+
decodePrefixedString(this->allocated_, this->value_.string_, &length, begin);
655+
*end = *begin + length;
656656
return true;
657657
}
658658

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

1006-
void Value::initBasic(ValueType vtype, bool allocated) {
1007-
type_ = vtype;
1006+
void Value::initBasic(ValueType type, bool allocated) {
1007+
type_ = type;
10081008
allocated_ = allocated;
10091009
comments_ = 0;
10101010
start_ = 0;
@@ -1101,13 +1101,13 @@ Value& Value::resolveReference(const char* key) {
11011101
}
11021102

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

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

1129-
Value const* Value::find(char const* key, char const* cend) const {
1129+
Value const* Value::find(char const* begin, char const* end) const {
11301130
JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == objectValue,
11311131
"in Json::Value::find(key, end, found): requires "
11321132
"objectValue or nullValue");
11331133
if (type_ == nullValue)
11341134
return NULL;
1135-
CZString actualKey(key, static_cast<unsigned>(cend - key),
1135+
CZString actualKey(begin, static_cast<unsigned>(end - begin),
11361136
CZString::noDuplication);
11371137
ObjectValues::const_iterator it = value_.map_->find(actualKey);
11381138
if (it == value_.map_->end())
@@ -1184,10 +1184,10 @@ Value& Value::append(Value&& value) {
11841184
}
11851185
#endif
11861186

1187-
Value Value::get(char const* key,
1188-
char const* cend,
1187+
Value Value::get(char const* begin,
1188+
char const* end,
11891189
Value const& defaultValue) const {
1190-
Value const* found = find(key, cend);
1190+
Value const* found = find(begin, end);
11911191
return !found ? defaultValue : *found;
11921192
}
11931193
Value Value::get(char const* key, Value const& defaultValue) const {
@@ -1197,11 +1197,11 @@ Value Value::get(JSONCPP_STRING const& key, Value const& defaultValue) const {
11971197
return get(key.data(), key.data() + key.length(), defaultValue);
11981198
}
11991199

1200-
bool Value::removeMember(const char* key, const char* cend, Value* removed) {
1200+
bool Value::removeMember(const char* begin, const char* end, Value* removed) {
12011201
if (type_ != objectValue) {
12021202
return false;
12031203
}
1204-
CZString actualKey(key, static_cast<unsigned>(cend - key),
1204+
CZString actualKey(begin, static_cast<unsigned>(end - begin),
12051205
CZString::noDuplication);
12061206
ObjectValues::iterator it = value_.map_->find(actualKey);
12071207
if (it == value_.map_->end())
@@ -1264,8 +1264,8 @@ Value Value::get(const CppTL::ConstString& key,
12641264
}
12651265
#endif
12661266

1267-
bool Value::isMember(char const* key, char const* cend) const {
1268-
Value const* value = find(key, cend);
1267+
bool Value::isMember(char const* begin, char const* end) const {
1268+
Value const* value = find(begin, end);
12691269
return NULL != value;
12701270
}
12711271
bool Value::isMember(char const* key) const {

src/lib_json/json_writer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ bool StyledWriter::hasCommentForValue(const Value& value) {
643643
// Class StyledStreamWriter
644644
// //////////////////////////////////////////////////////////////////
645645

646-
StyledStreamWriter::StyledStreamWriter(JSONCPP_STRING indentation)
646+
StyledStreamWriter::StyledStreamWriter(const JSONCPP_STRING& indentation)
647647
: document_(NULL), rightMargin_(74), indentation_(indentation),
648648
addChildValues_(), indented_(false) {}
649649

@@ -1245,10 +1245,10 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings) {
12451245
//! [StreamWriterBuilderDefaults]
12461246
}
12471247

1248-
JSONCPP_STRING writeString(StreamWriter::Factory const& builder,
1248+
JSONCPP_STRING writeString(StreamWriter::Factory const& factory,
12491249
Value const& root) {
12501250
JSONCPP_OSTRINGSTREAM sout;
1251-
StreamWriterPtr const writer(builder.newStreamWriter());
1251+
StreamWriterPtr const writer(factory.newStreamWriter());
12521252
writer->write(root, &sout);
12531253
return sout.str();
12541254
}

0 commit comments

Comments
 (0)