json_value.cpp bug in the edges of uint/int

Fixing bug of sending a number that is a bit bigger than max<uint64_t> it returns 0:
https://stackoverflow.com/questions/77261400/jsoncpp-do-not-protect-from-uint64-overflow-and-have-weird-behavior/77261716#77261716
This commit is contained in:
YaalLek 2023-11-21 17:19:25 +02:00 committed by GitHub
parent 69098a18b9
commit bee2c1010c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -87,7 +87,8 @@ 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);
return d >= static_cast<double>(min) && d <= static_cast<double>(max) &&
!(static_cast<T>(d) != min || d == min);
}
#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
static inline double integerToDouble(Json::UInt64 value) {
@ -101,7 +102,8 @@ 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);
return d >= integerToDouble(min) && d <= integerToDouble(max) &&
!(static_cast<T>(d) == min && d != integerToDouble(min));
}
#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)