Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uint64 overflow doesn't always throws exception #1545

Closed
YaalLek opened this issue Jul 2, 2024 · 0 comments
Closed

Uint64 overflow doesn't always throws exception #1545

YaalLek opened this issue Jul 2, 2024 · 0 comments

Comments

@YaalLek
Copy link
Contributor

YaalLek commented Jul 2, 2024

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):

  • OS: Ubuntu 2020

Additional context
Suggested code for fix is here: #1519

@baylesj baylesj closed this as completed Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants