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

Wrong parsing of int64 values nearest of limit #3126

Closed
1 of 3 tasks
gysevvlad opened this issue Nov 7, 2021 · 2 comments
Closed
1 of 3 tasks

Wrong parsing of int64 values nearest of limit #3126

gysevvlad opened this issue Nov 7, 2021 · 2 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@gysevvlad
Copy link

gysevvlad commented Nov 7, 2021

What is the issue you have?

For number "9223372036854775808" (int64_t::max() + 1) I expects that json.get<nlohmann::json::number_integer_t>() throws exception.

Can you provide a small but working code example?

https://godbolt.org/z/v4ejoPszf

#include "nlohmann/json.hpp"
#include <iostream>

int main(int, char **) {
  auto json = nlohmann::json::parse(R"({ "package size": 9223372036854775808 })");
  auto [key, number] = *json.items().begin();
  std::cout << number.get<nlohmann::json::number_integer_t>() << std::endl;
  return 0;
}

What is the expected behavior?

I expects to get exception.

And what is the actual behavior instead?

Program stdout
-9223372036854775808

Which compiler and operating system are you using?

  • Compiler: clang-12
  • Operating system: Ubuntu, RedHat

Which version of the library did you use?

  • latest release version 3.10.4
  • other release - please state the version: ___
  • the develop branch
@gysevvlad
Copy link
Author

Also it's relevant for is_number_integer check:

#include "nlohmann/json.hpp"
#include <iostream>

int main(int, char **) {
  auto json = nlohmann::json::parse(R"({ "package size": 9223372036854775808 })");
  auto [key, number] = *json.items().begin();
  if (number.is_number_integer()) {
    std::cout << number.get<nlohmann::json::number_integer_t>() << std::endl;
  }
  return 0;
}
bash :> ./build/default/main
-9223372036854775808

@nlohmann
Copy link
Owner

nlohmann commented Nov 7, 2021

The library differentiates between signed and unsigned integers (see https://json.nlohmann.me/features/types/number_handling/). is_number_integer() returns true for either integer type (see https://json.nlohmann.me/api/basic_json/is_number_integer/).

So with get<nlohmann::json::number_integer_t>() you actually cast an std::uint64_t to a std::int64_t.

#include <nlohmann/json.hpp>

int main() {
    const auto j = nlohmann::json::parse("9223372036854775808");
    assert(j.is_number_unsigned());
    std::uint64_t n = j;
    assert(n == 9223372036854775808U);
}

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed kind: bug labels Nov 7, 2021
@nlohmann nlohmann closed this as completed Nov 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants