Skip to content

Commit 3dbaa54

Browse files
committed
Fix outputting extreme integer values in edge cases
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).
1 parent 13684f2 commit 3dbaa54

2 files changed

Lines changed: 2 additions & 2 deletions

File tree

include/nlohmann/detail/output/serializer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ class serializer
630630
if (is_negative)
631631
{
632632
*buffer_ptr = '-';
633-
abs_value = static_cast<number_unsigned_t>(std::abs(static_cast<std::intmax_t>(x)));
633+
abs_value = static_cast<number_unsigned_t>(-x);
634634

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

single_include/nlohmann/json.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14264,7 +14264,7 @@ class serializer
1426414264
if (is_negative)
1426514265
{
1426614266
*buffer_ptr = '-';
14267-
abs_value = static_cast<number_unsigned_t>(std::abs(static_cast<std::intmax_t>(x)));
14267+
abs_value = static_cast<number_unsigned_t>(-x);
1426814268

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

0 commit comments

Comments
 (0)