Skip to content

Commit

Permalink
Fix outputting extreme integer values in edge cases
Browse files Browse the repository at this point in the history
For some gcc version (Ubuntu 5.5.0-12ubuntu1~16.04) the existing code
crashes when the minimum value of int64_t is outputted.

Rewrite the code to be less complicated to avoid it. This partially
reverts what was done in 546e2cb (:rotating_light: fixed some warnings,
2019-03-13).
  • Loading branch information
t-b committed Aug 23, 2019
1 parent 13684f2 commit 3dbaa54
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 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 @@ -630,7 +630,7 @@ class serializer
if (is_negative)
{
*buffer_ptr = '-';
abs_value = static_cast<number_unsigned_t>(std::abs(static_cast<std::intmax_t>(x)));
abs_value = static_cast<number_unsigned_t>(-x);

// 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 @@ -14264,7 +14264,7 @@ class serializer
if (is_negative)
{
*buffer_ptr = '-';
abs_value = static_cast<number_unsigned_t>(std::abs(static_cast<std::intmax_t>(x)));
abs_value = static_cast<number_unsigned_t>(-x);

// account one more byte for the minus sign
n_chars = 1 + count_digits(abs_value);
Expand Down

0 comments on commit 3dbaa54

Please sign in to comment.