open-source-parsers / jsoncpp

A C++ library for interacting with JSON.
Other
8.06k stars 2.63k forks source link

Uint64 overflow doesn't always throws exception #1545

Open YaalLek opened 2 months ago

YaalLek commented 2 months ago

Describe the bug When trying to read values as uint64, for numbers that are in uint64 range are working properly. Numbers above range but between [2^64,2^64+2^11] return 0. Numbers above 2^64+2^11 throws an exception.

To Reproduce Code for example:

#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;
}

Expected behavior An error should be thrown also for numbers between [2^64,2^64+2^11].

Desktop (please complete the following information):

Additional context Suggested code for fix is here: https://github.com/open-source-parsers/jsoncpp/pull/1519