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

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

Closed
MichaelHuth opened this issue Jul 5, 2017 · 4 comments
Closed

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

MichaelHuth opened this issue Jul 5, 2017 · 4 comments

Comments

@MichaelHuth
Copy link

MichaelHuth commented Jul 5, 2017

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.

@nlohmann
Copy link
Owner

nlohmann commented Jul 5, 2017

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.

@nlohmann
Copy link
Owner

nlohmann commented Jul 5, 2017

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

@nlohmann
Copy link
Owner

nlohmann commented Jul 6, 2017

Did this answer your question?

@nlohmann nlohmann added the state: please discuss please discuss the issue or vote for your favorite option label Jul 6, 2017
@nlohmann
Copy link
Owner

nlohmann commented Jul 8, 2017

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.

@nlohmann nlohmann removed the state: please discuss please discuss the issue or vote for your favorite option label Dec 4, 2017
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