I am playing around with JsonCpp and have noticed weird behavior when getting to the end of uint64 range.
Numbers in range works properly.
Numbers above range but between [2**64,2**64+2**11] return 0.
Numbers above 2**64+2**11 throws an exception.
Is that expected? Can I configure JsonCpp to do those validations for me?
Thanks in advance!
Below is a code snippet that demonstrate the issue.
#include <iostream>
#include <jsoncpp/json/json.h>
#include <jsoncpp/json/reader.h>
#include <jsoncpp/json/value.h>
int main() {
//std::string uint64Str = "1234"; // any number in range [0, 2**64-1] works correctly (prints uint64Str)
//std::string uint64Str = "18446744073709551615"; // any number in range [0, 2**64-1] works correctly, including 2**64-1 (prints uint64Str)
//std::string uint64Str = "18446744073709551616"; //max uint64 + 1 print 0
//std::string uint64Str = "18446744073709551617"; //max uint64 + 2 print 0
// ...
//std::string uint64Str = "18446744073709553663"; //max uint64 + 2048 print 0
//std::string uint64Str = "18446744073709553664"; //max uint64 + 2049 print 0
std::string uint64Str = "18446744073709553665"; //max uint64 + 2050 (or more) throws instance of 'Json::LogicError' what(): double out of UInt64 range
Json::Value root;
Json::Reader reader;
bool parsingResult = reader.parse("{\"key\":"+uint64Str+"}", root);
std::cout<<root["key"].asUInt64()<<std::endl;
return 0;
}
Thanks to TedLyngmo for the guidance.
After his answer and additional research. Numbers that are bigger than max are stored in a real (double).
And then this code runs:
I would expect the function
InRangeto fail on numbers out of range. However its implementation is:It converts max to double and it rounds max up (to
2**64+2) and if d is betweenmax<uint64> to 2**62+2**11is is also rounded (to2**64+2). So it thinks it is in range. When converting d to uint64_t you get max+1 which is 0.The function
InRangedo mention that is in not exact... yet, I consider that to be a bug in the library.The solution that I am going to use is:
Update (25 Nov 2023): See suggested fix here: https://github.com/open-source-parsers/jsoncpp/pull/1519