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

Small bugs in json.hpp (get_number) and unit.cpp (non-standard integer type test) #199

Closed
twelsby opened this issue Jan 30, 2016 · 1 comment

Comments

@twelsby
Copy link
Contributor

twelsby commented Jan 30, 2016

There is a small bug in the following area of json.cpp:

if (attempt_cast(std::strtoll(reinterpret_cast<typename string_t::const_pointer>(m_start), 
                             &endptr, 10), result.m_value.number_unsigned))

Here result.m_value.number_unsigned should be result.m_value.number_integer.

Then in unit.hpp there is another bug:

// integer object creation - expected to wrap and still be stored as an integer
j = -2147483649LL; // -2^31-1
CHECK(static_cast<int>(j.type()) == static_cast<int>(custom_json::value_t::number_integer));
CHECK(j.get<int32_t>() == 2147483647.0);  // Wrap

// integer parsing - expected to overflow and be stored as a float
j = custom_json::parse("-2147483648"); // -2^31
CHECK(static_cast<int>(j.type()) == static_cast<int>(custom_json::value_t::number_float));
CHECK(j.get<float>() == -2147483648.0);

The second test for parsing uses an incorrect value. Instead of -2147483648 it should read -2147483649.

The previous value -2147483648 should not have wrapped as was intended however the bug in json.hpp had the effect of causing the cast to fail anyway. The second error effectively hid the first error so the unit test passed.

One other small error is that all of the floating point comparisons in this section should have an 'f' after the literals otherwise they are double literals - so the above comparison would read CHECK(j.get<float>() == -2147483650.0f.

twelsby pushed a commit to twelsby/json that referenced this issue Jan 30, 2016
nlohmann added a commit that referenced this issue Jan 30, 2016
Fixed issue #199 - Small bugs in json.hpp (get_number) and unit.cpp (non-standard integer type test)
@nlohmann nlohmann added this to the Release 2.0.0 milestone Jan 30, 2016
@nlohmann
Copy link
Owner

Thanks a lot!

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

No branches or pull requests

2 participants