Skip to content

Commit

Permalink
🐛 fixed integer overflow in dump function #1447
Browse files Browse the repository at this point in the history
Closes #1447.
  • Loading branch information
nlohmann committed Jan 20, 2019
1 parent e17e0d0 commit 6de4df2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion include/nlohmann/detail/output/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ class serializer
if (is_negative)
{
*buffer_ptr = '-';
abs_value = static_cast<number_unsigned_t>(0 - x);
abs_value = static_cast<number_unsigned_t>(-1 - x) + 1;

// account one more byte for the minus sign
n_chars = 1 + count_digits(abs_value);
Expand Down
2 changes: 1 addition & 1 deletion single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11510,7 +11510,7 @@ class serializer
if (is_negative)
{
*buffer_ptr = '-';
abs_value = static_cast<number_unsigned_t>(0 - x);
abs_value = static_cast<number_unsigned_t>(-1 - x) + 1;

// account one more byte for the minus sign
n_chars = 1 + count_digits(abs_value);
Expand Down
11 changes: 9 additions & 2 deletions test/src/unit-regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,8 @@ TEST_CASE("regression tests")
SECTION("test case in issue #1445")
{
nlohmann::json dump_test;
const int data[] = {
const int data[] =
{
109, 108, 103, 125, -122, -53, 115,
18, 3, 0, 102, 19, 1, 15,
-110, 13, -3, -1, -81, 32, 2,
Expand All @@ -1761,14 +1762,20 @@ TEST_CASE("regression tests")
-54, -28, -26
};
std::string s;
for (int i=0; i<sizeof(data)/sizeof(int); i++)
for (int i = 0; i < sizeof(data) / sizeof(int); i++)
{
s += static_cast<char>(data[i]);
}
dump_test["1"] = s;
dump_test.dump(-1, ' ', true, nlohmann::json::error_handler_t::replace);
}
}

SECTION("issue #1447 - Integer Overflow (OSS-Fuzz 12506)")
{
json j = json::parse("[-9223372036854775808]");
CHECK(j.dump() == "[-9223372036854775808]");
}
}

TEST_CASE("regression tests, exceptions dependent", "[!throws]")
Expand Down

0 comments on commit 6de4df2

Please sign in to comment.