Skip to content

Commit b860cc3

Browse files
committed
Merge pull request open-source-parsers#414 from cdunn2001/conversion-errors
Fix conversion warnings/errors
2 parents baefec7 + fef4b75 commit b860cc3

File tree

9 files changed

+65
-57
lines changed

9 files changed

+65
-57
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ endif()
103103

104104
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
105105
# using regular Clang or AppleClang
106-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wconversion -Wshadow -Wno-sign-conversion")
106+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wconversion -Wshadow -Wno-sign-conversion -Werror=conversion")
107107
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
108108
# using GCC
109-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wconversion -Wshadow -Wextra")
109+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wconversion -Wshadow -Wextra -Werror=conversion")
110110
# not yet ready for -Wsign-conversion
111111

112112
if (JSONCPP_WITH_STRICT_ISO AND NOT JSONCPP_WITH_WARNING_AS_ERROR)

include/json/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#ifndef JSON_CONFIG_H_INCLUDED
77
#define JSON_CONFIG_H_INCLUDED
8+
#include <stddef.h>
89

910
/// If defined, indicates that json library is embedded in CppTL library.
1011
//# define JSON_IN_CPPTL 1

include/json/reader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class JSON_API Reader {
4242
*
4343
*/
4444
struct StructuredError {
45-
size_t offset_start;
46-
size_t offset_limit;
45+
ptrdiff_t offset_start;
46+
ptrdiff_t offset_limit;
4747
std::string message;
4848
};
4949

include/json/value.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,10 @@ Json::Value obj_value(Json::objectValue); // {}
554554

555555
// Accessors for the [start, limit) range of bytes within the JSON text from
556556
// which this value was parsed, if any.
557-
void setOffsetStart(size_t start);
558-
void setOffsetLimit(size_t limit);
559-
size_t getOffsetStart() const;
560-
size_t getOffsetLimit() const;
557+
void setOffsetStart(ptrdiff_t start);
558+
void setOffsetLimit(ptrdiff_t limit);
559+
ptrdiff_t getOffsetStart() const;
560+
ptrdiff_t getOffsetLimit() const;
561561

562562
private:
563563
void initBasic(ValueType type, bool allocated = false);
@@ -598,8 +598,8 @@ Json::Value obj_value(Json::objectValue); // {}
598598

599599
// [start, limit) byte offsets in the source JSON text from which this Value
600600
// was extracted.
601-
size_t start_;
602-
size_t limit_;
601+
ptrdiff_t start_;
602+
ptrdiff_t limit_;
603603
};
604604

605605
/** \brief Experimental and untested: represents an element of the "path" to

include/json/writer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ class JSON_API StyledWriter : public Writer {
238238
ChildValues childValues_;
239239
std::string document_;
240240
std::string indentString_;
241-
int rightMargin_;
242-
int indentSize_;
241+
unsigned int rightMargin_;
242+
unsigned int indentSize_;
243243
bool addChildValues_;
244244
};
245245

@@ -302,7 +302,7 @@ class JSON_API StyledStreamWriter {
302302
ChildValues childValues_;
303303
std::ostream* document_;
304304
std::string indentString_;
305-
int rightMargin_;
305+
unsigned int rightMargin_;
306306
std::string indentation_;
307307
bool addChildValues_ : 1;
308308
bool indented_ : 1;

src/jsontestrunner/main.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@ static std::string readInputTestFile(const char* path) {
5757
if (!file)
5858
return std::string("");
5959
fseek(file, 0, SEEK_END);
60-
long size = ftell(file);
60+
long const size = ftell(file);
61+
unsigned long const usize = static_cast<unsigned long const>(size);
6162
fseek(file, 0, SEEK_SET);
6263
std::string text;
6364
char* buffer = new char[size + 1];
6465
buffer[size] = 0;
65-
if (fread(buffer, 1, size, file) == (unsigned long)size)
66+
if (fread(buffer, 1, usize, file) == usize)
6667
text = buffer;
6768
fclose(file);
6869
delete[] buffer;
@@ -104,8 +105,8 @@ printValueTree(FILE* fout, Json::Value& value, const std::string& path = ".") {
104105
break;
105106
case Json::arrayValue: {
106107
fprintf(fout, "%s=[]\n", path.c_str());
107-
int size = value.size();
108-
for (int index = 0; index < size; ++index) {
108+
Json::ArrayIndex size = value.size();
109+
for (Json::ArrayIndex index = 0; index < size; ++index) {
109110
static char buffer[16];
110111
#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
111112
sprintf_s(buffer, sizeof(buffer), "[%d]", index);

src/lib_json/json_reader.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ bool Reader::readComment() {
368368

369369
static std::string normalizeEOL(Reader::Location begin, Reader::Location end) {
370370
std::string normalized;
371-
normalized.reserve(end - begin);
371+
normalized.reserve(static_cast<size_t>(end - begin));
372372
Reader::Location current = begin;
373373
while (current != end) {
374374
char c = *current++;
@@ -578,7 +578,7 @@ bool Reader::decodeNumber(Token& token, Value& decoded) {
578578
Char c = *current++;
579579
if (c < '0' || c > '9')
580580
return decodeDouble(token, decoded);
581-
Value::UInt digit(c - '0');
581+
Value::UInt digit(static_cast<Value::UInt>(c - '0'));
582582
if (value >= threshold) {
583583
// We've hit or exceeded the max value divided by 10 (rounded down). If
584584
// a) we've only just touched the limit, b) this is the last digit, and
@@ -636,7 +636,7 @@ bool Reader::decodeString(Token& token) {
636636
}
637637

638638
bool Reader::decodeString(Token& token, std::string& decoded) {
639-
decoded.reserve(token.end_ - token.start_ - 2);
639+
decoded.reserve(static_cast<size_t>(token.end_ - token.start_ - 2));
640640
Location current = token.start_ + 1; // skip '"'
641641
Location end = token.end_ - 1; // do not include '"'
642642
while (current != end) {
@@ -720,13 +720,13 @@ bool Reader::decodeUnicodeCodePoint(Token& token,
720720
bool Reader::decodeUnicodeEscapeSequence(Token& token,
721721
Location& current,
722722
Location end,
723-
unsigned int& unicode) {
723+
unsigned int& ret_unicode) {
724724
if (end - current < 4)
725725
return addError(
726726
"Bad unicode escape sequence in string: four digits expected.",
727727
token,
728728
current);
729-
unicode = 0;
729+
int unicode = 0;
730730
for (int index = 0; index < 4; ++index) {
731731
Char c = *current++;
732732
unicode *= 16;
@@ -742,6 +742,7 @@ bool Reader::decodeUnicodeEscapeSequence(Token& token,
742742
token,
743743
current);
744744
}
745+
ret_unicode = static_cast<unsigned int>(unicode);
745746
return true;
746747
}
747748

@@ -756,7 +757,7 @@ Reader::addError(const std::string& message, Token& token, Location extra) {
756757
}
757758

758759
bool Reader::recoverFromError(TokenType skipUntilToken) {
759-
int errorCount = int(errors_.size());
760+
size_t const errorCount = errors_.size();
760761
Token skip;
761762
for (;;) {
762763
if (!readToken(skip))
@@ -851,7 +852,7 @@ std::vector<Reader::StructuredError> Reader::getStructuredErrors() const {
851852
}
852853

853854
bool Reader::pushError(const Value& value, const std::string& message) {
854-
size_t length = end_ - begin_;
855+
ptrdiff_t const length = end_ - begin_;
855856
if(value.getOffsetStart() > length
856857
|| value.getOffsetLimit() > length)
857858
return false;
@@ -868,7 +869,7 @@ bool Reader::pushError(const Value& value, const std::string& message) {
868869
}
869870

870871
bool Reader::pushError(const Value& value, const std::string& message, const Value& extra) {
871-
size_t length = end_ - begin_;
872+
ptrdiff_t const length = end_ - begin_;
872873
if(value.getOffsetStart() > length
873874
|| value.getOffsetLimit() > length
874875
|| extra.getOffsetLimit() > length)
@@ -918,8 +919,8 @@ class OurReader {
918919
typedef char Char;
919920
typedef const Char* Location;
920921
struct StructuredError {
921-
size_t offset_start;
922-
size_t offset_limit;
922+
ptrdiff_t offset_start;
923+
ptrdiff_t offset_limit;
923924
std::string message;
924925
};
925926

@@ -1560,7 +1561,7 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
15601561
Char c = *current++;
15611562
if (c < '0' || c > '9')
15621563
return decodeDouble(token, decoded);
1563-
Value::UInt digit(c - '0');
1564+
Value::UInt digit(static_cast<Value::UInt>(c - '0'));
15641565
if (value >= threshold) {
15651566
// We've hit or exceeded the max value divided by 10 (rounded down). If
15661567
// a) we've only just touched the limit, b) this is the last digit, and
@@ -1596,12 +1597,13 @@ bool OurReader::decodeDouble(Token& token, Value& decoded) {
15961597
double value = 0;
15971598
const int bufferSize = 32;
15981599
int count;
1599-
int length = int(token.end_ - token.start_);
1600+
ptrdiff_t const length = token.end_ - token.start_;
16001601

16011602
// Sanity check to avoid buffer overflow exploits.
16021603
if (length < 0) {
16031604
return addError("Unable to parse token length", token);
16041605
}
1606+
size_t const ulength = static_cast<size_t>(length);
16051607

16061608
// Avoid using a string constant for the format control string given to
16071609
// sscanf, as this can cause hard to debug crashes on OS X. See here for more
@@ -1612,7 +1614,7 @@ bool OurReader::decodeDouble(Token& token, Value& decoded) {
16121614

16131615
if (length <= bufferSize) {
16141616
Char buffer[bufferSize + 1];
1615-
memcpy(buffer, token.start_, length);
1617+
memcpy(buffer, token.start_, ulength);
16161618
buffer[length] = 0;
16171619
count = sscanf(buffer, format, &value);
16181620
} else {
@@ -1640,7 +1642,7 @@ bool OurReader::decodeString(Token& token) {
16401642
}
16411643

16421644
bool OurReader::decodeString(Token& token, std::string& decoded) {
1643-
decoded.reserve(token.end_ - token.start_ - 2);
1645+
decoded.reserve(static_cast<size_t>(token.end_ - token.start_ - 2));
16441646
Location current = token.start_ + 1; // skip '"'
16451647
Location end = token.end_ - 1; // do not include '"'
16461648
while (current != end) {
@@ -1724,13 +1726,13 @@ bool OurReader::decodeUnicodeCodePoint(Token& token,
17241726
bool OurReader::decodeUnicodeEscapeSequence(Token& token,
17251727
Location& current,
17261728
Location end,
1727-
unsigned int& unicode) {
1729+
unsigned int& ret_unicode) {
17281730
if (end - current < 4)
17291731
return addError(
17301732
"Bad unicode escape sequence in string: four digits expected.",
17311733
token,
17321734
current);
1733-
unicode = 0;
1735+
int unicode = 0;
17341736
for (int index = 0; index < 4; ++index) {
17351737
Char c = *current++;
17361738
unicode *= 16;
@@ -1746,6 +1748,7 @@ bool OurReader::decodeUnicodeEscapeSequence(Token& token,
17461748
token,
17471749
current);
17481750
}
1751+
ret_unicode = static_cast<unsigned int>(unicode);
17491752
return true;
17501753
}
17511754

@@ -1760,7 +1763,7 @@ OurReader::addError(const std::string& message, Token& token, Location extra) {
17601763
}
17611764

17621765
bool OurReader::recoverFromError(TokenType skipUntilToken) {
1763-
int errorCount = int(errors_.size());
1766+
size_t errorCount = errors_.size();
17641767
Token skip;
17651768
for (;;) {
17661769
if (!readToken(skip))
@@ -1850,7 +1853,7 @@ std::vector<OurReader::StructuredError> OurReader::getStructuredErrors() const {
18501853
}
18511854

18521855
bool OurReader::pushError(const Value& value, const std::string& message) {
1853-
size_t length = end_ - begin_;
1856+
ptrdiff_t length = end_ - begin_;
18541857
if(value.getOffsetStart() > length
18551858
|| value.getOffsetLimit() > length)
18561859
return false;
@@ -1867,7 +1870,7 @@ bool OurReader::pushError(const Value& value, const std::string& message) {
18671870
}
18681871

18691872
bool OurReader::pushError(const Value& value, const std::string& message, const Value& extra) {
1870-
size_t length = end_ - begin_;
1873+
ptrdiff_t length = end_ - begin_;
18711874
if(value.getOffsetStart() > length
18721875
|| value.getOffsetLimit() > length
18731876
|| extra.getOffsetLimit() > length)

src/lib_json/json_value.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ const LargestUInt Value::maxLargestUInt = LargestUInt(-1);
5555
#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
5656
template <typename T, typename U>
5757
static inline bool InRange(double d, T min, U max) {
58+
// The casts can lose precision, but we are looking only for
59+
// an approximate range. Might fail on edge cases though. ~cdunn
60+
//return d >= static_cast<double>(min) && d <= static_cast<double>(max);
5861
return d >= min && d <= max;
5962
}
6063
#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
@@ -1332,13 +1335,13 @@ std::string Value::getComment(CommentPlacement placement) const {
13321335
return "";
13331336
}
13341337

1335-
void Value::setOffsetStart(size_t start) { start_ = start; }
1338+
void Value::setOffsetStart(ptrdiff_t start) { start_ = start; }
13361339

1337-
void Value::setOffsetLimit(size_t limit) { limit_ = limit; }
1340+
void Value::setOffsetLimit(ptrdiff_t limit) { limit_ = limit; }
13381341

1339-
size_t Value::getOffsetStart() const { return start_; }
1342+
ptrdiff_t Value::getOffsetStart() const { return start_; }
13401343

1341-
size_t Value::getOffsetLimit() const { return limit_; }
1344+
ptrdiff_t Value::getOffsetLimit() const { return limit_; }
13421345

13431346
std::string Value::toStyledString() const {
13441347
StyledWriter writer;

0 commit comments

Comments
 (0)