From 974c0d83f0ac7edd7bea3f7c1604a555fd8dc56c Mon Sep 17 00:00:00 2001 From: b84330808 Date: Thu, 5 Nov 2026 11:25:18 +0900 Subject: [PATCH] fix: don't rely on an out-of-range cast in InRange() InRange() rejected a double just over a 64-bit limit by casting it to the integer type and comparing with min. That cast is undefined, and its result differs by compiler and architecture. With clang on x86-64, Value(18446744073709551616.0).asUInt64() gave 2^63 instead of throwing, which is #1545 again. The same check also caught every value between 0 and 1, since those truncate to the unsigned min. Value(0.5).asUInt() threw and Value(0.5).isConvertibleTo(uintValue) was false, while 1.5 converts to 1. Compare against the limit instead: strictly when it isn't exactly representable as a double, as isInt64() and isUInt64() already do. --- src/lib_json/json_value.cpp | 20 ++++++++++++++------ src/test_lib_json/main.cpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 5d3ddfd8a..4e1d31084 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -85,10 +86,14 @@ Value const& Value::nullRef = Value::nullSingleton(); #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) template 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(min) && d <= static_cast(max) && - !(static_cast(d) == min && d != static_cast(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(max); + const bool upperIsExact = + std::numeric_limits::digits <= std::numeric_limits::digits; + return d >= static_cast(min) && + (upperIsExact ? d <= upper : d < upper); } #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) static inline double integerToDouble(Json::UInt64 value) { @@ -102,8 +107,11 @@ template static inline double integerToDouble(T value) { template static inline bool InRange(double d, T min, U max) { - return d >= integerToDouble(min) && d <= integerToDouble(max) && - !(static_cast(d) == min && d != integerToDouble(min)); + // See the comment on the other InRange(). + const double upper = integerToDouble(max); + const bool upperIsExact = + std::numeric_limits::digits <= std::numeric_limits::digits; + return d >= integerToDouble(min) && (upperIsExact ? d <= upper : d < upper); } #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 313e9f947..021241ac2 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -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); @@ -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);