Skip to content

No exception raised for Out Of Range input of numbers #647

Closed

Description

@MichaelHuth

Hello,

if one has values that are beyond the range of the target type no exception is raised.

e.g. this code with a value of UINT64_MAX+1 and 10000 for the target of a signed char outputs 0 and 16 respectively.

	json jtest = json::parse("{\"value\" : 18446744073709551617}");
	std::cout << std::to_string(jtest["value"].get<unsigned long long int>()) << "\n";
	jtest = json::parse("{\"value\" : 10000}");
	std::cout << std::to_string(jtest["value"].get<signed char>()) << "\n";

Both cases should throw an exception.

Best regards.

Activity

  1. nlohmann commented on Jul 5, 2017

    @nlohmann
    Owner

    The first number is parsed as double and the second as uint64_t. Calling get does not check for overflows - it is just like writing int i = 12345678901235.1235567.

  2. nlohmann commented on Jul 5, 2017

    @nlohmann
    Owner

    Remark: an exception is thrown by the parser if the given number does not fit neither the integer types nor the floating-point type.

  3. nlohmann commented on Jul 6, 2017

    @nlohmann
    Owner

    Did this answer your question?

  4. nlohmann commented on Jul 8, 2017

    @nlohmann
    Owner

    In fact, Xcode's undefined behavior sanitizer detects the first issue:

    /Users/niels/Documents/development/debug_lexer/debug_lexer/json.hpp:949:23: runtime error: 1.84467e+19 is outside the range of representable values of type 'unsigned long long'
    SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /Users/niels/Documents/development/debug_lexer/debug_lexer/json.hpp:949:23 in
    

    However, this is not inside the library, but in the requested conversion from double to unsigned long long. I'm not sure whether adding runtime checks to the library would be helpful here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions