Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/libfc/src/io/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ namespace fc
{
int64_t i = v.as_int64();
if( format == json::output_formatting::stringify_large_ints_and_doubles &&
i > 0xffffffff )
(i * ((i>0)-(i<0)) > 0xffffffff))
Copy link
Copy Markdown
Contributor

@heifner heifner Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this construct rather odd and I am a bit surprised that compilers would not give a warning about bool used in arithmetic statement.

Seems like: (i > 0xffffffff) || (i < -0xffffffff) would be clearer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about std::abs(i) > 0xffffffff
probably cmath should be included

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting to uint64_t should also do the job, i.e. static_cast<uint64_t>(i) > 0xffffffff

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, all great suggestions! Changed.

os << '"'<<v.as_string()<<'"';
else
os << i;
Expand Down
21 changes: 21 additions & 0 deletions libraries/libfc/test/io/test_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ BOOST_AUTO_TEST_CASE(to_string_test)
BOOST_CHECK_NO_THROW(length_exception_in_mid_str = json::to_string( v, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit));
BOOST_CHECK_EQUAL(length_exception_in_mid_str, "\"" + json_test_util::repeat_chars + "\"");
}
{
variant v(4294967296); // 0xFFFFFFFF + 1
std::string large_int = json::to_string( v, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit);
BOOST_CHECK_EQUAL(large_int, "\"4294967296\"");

variant v1(4294967295); // 0xFFFFFFFF
std::string normal_int = json::to_string( v1, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit);
BOOST_CHECK_EQUAL(normal_int, "4294967295");

variant v2(-4294967296);
std::string large_int_neg = json::to_string( v2, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit);
BOOST_CHECK_EQUAL(large_int_neg, "\"-4294967296\"");

variant v3(-4294967295);
std::string normal_int_neg = json::to_string( v3, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit);
BOOST_CHECK_EQUAL(normal_int_neg, "-4294967295");

variant v4(-90909090909090909);
std::string super_neg = json::to_string( v4, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit);
BOOST_CHECK_EQUAL(super_neg, "\"-90909090909090909\"");
}
}
{ // to_string( const variant& v, const yield_function_t& yield, output_formatting format = stringify_large_ints_and_doubles);
const variant v(json_test_util::repeat_chars);
Expand Down