Skip to content
Open
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
20 changes: 14 additions & 6 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <cstddef>
#include <cstring>
#include <iostream>
#include <limits>
#include <sstream>
#include <utility>

Expand Down Expand Up @@ -85,10 +86,14 @@ Value const& Value::nullRef = Value::nullSingleton();
#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
template <typename T, typename U>
static inline bool InRange(double d, T min, U max) {
// The casts can lose precision, but we are looking only for
// an approximate range. Might fail on edge cases though. ~cdunn
return d >= static_cast<double>(min) && d <= static_cast<double>(max) &&
!(static_cast<U>(d) == min && d != static_cast<double>(min));
// A 64-bit max is not exactly representable as a double: it rounds up to 2^63
// or 2^64, which is out of range. Casting that to U is undefined behavior, so
// it has to be rejected here.
const double upper = static_cast<double>(max);
const bool upperIsExact =
std::numeric_limits<U>::digits <= std::numeric_limits<double>::digits;
return d >= static_cast<double>(min) &&
(upperIsExact ? d <= upper : d < upper);
}
#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
static inline double integerToDouble(Json::UInt64 value) {
Expand All @@ -102,8 +107,11 @@ template <typename T> static inline double integerToDouble(T value) {

template <typename T, typename U>
static inline bool InRange(double d, T min, U max) {
return d >= integerToDouble(min) && d <= integerToDouble(max) &&
!(static_cast<U>(d) == min && d != integerToDouble(min));
// See the comment on the other InRange().
const double upper = integerToDouble(max);
const bool upperIsExact =
std::numeric_limits<U>::digits <= std::numeric_limits<double>::digits;
return d >= integerToDouble(min) && (upperIsExact ? d <= upper : d < upper);
}
#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)

Expand Down
33 changes: 33 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,13 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, nonIntegers) {
JSONTEST_ASSERT_EQUAL(true, val.asBool());
JSONTEST_ASSERT_EQUAL("1.5", val.asString());

// Below one: truncates to the lowest unsigned value, like 1.5 truncates to 1
val = Json::Value(0.5);

JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue));
JSONTEST_ASSERT_EQUAL(0, val.asUInt());
JSONTEST_ASSERT_EQUAL(0, val.asLargestUInt());

// Small negative number
val = Json::Value(-1.5);

Expand Down Expand Up @@ -2000,6 +2007,32 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, typeChecksThrowExceptions) {
#endif // JSON_USE_EXCEPTION
}

#if defined(JSON_HAS_INT64)
JSONTEST_FIXTURE_LOCAL(ValueTest, doubleConversionEdges) {
// The largest doubles in range convert exactly.
const double belowTwoTo63 = 9223372036854774784.0;
const double belowTwoTo64 = 18446744073709549568.0;
JSONTEST_ASSERT_EQUAL(Json::Int64(9223372036854774784),
Json::Value(belowTwoTo63).asInt64());
JSONTEST_ASSERT_EQUAL(Json::UInt64(18446744073709549568U),
Json::Value(belowTwoTo64).asUInt64());
JSONTEST_ASSERT_EQUAL(Json::UInt64(9223372036854775808U),
Json::Value(9223372036854775808.0).asUInt64());

#if JSON_USE_EXCEPTION
// 2^63 and 2^64 are one past the limits. They used to reach a cast with no
// defined result, and asUInt64() of 2^64 gave 2^63 when built with clang on
// x86-64.
const double twoTo63 = 9223372036854775808.0;
const double twoTo64 = 18446744073709551616.0;
JSONTEST_ASSERT_THROWS(Json::Value(twoTo63).asInt64());
JSONTEST_ASSERT_THROWS(Json::Value(twoTo63).asLargestInt());
JSONTEST_ASSERT_THROWS(Json::Value(twoTo64).asUInt64());
JSONTEST_ASSERT_THROWS(Json::Value(twoTo64).asLargestUInt());
#endif // JSON_USE_EXCEPTION
}
#endif // defined(JSON_HAS_INT64)

JSONTEST_FIXTURE_LOCAL(ValueTest, offsetAccessors) {
Json::Value x;
JSONTEST_ASSERT(x.getOffsetStart() == 0);
Expand Down