From 3dbaa54e075a1354c5245749c706d3c227bfb193 Mon Sep 17 00:00:00 2001 From: Thomas Braun Date: Fri, 23 Aug 2019 23:52:48 +0200 Subject: [PATCH] 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 546e2cbf (:rotating_light: fixed some warnings, 2019-03-13). --- include/nlohmann/detail/output/serializer.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index e6811ceb3b..55ee32c5bf 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -630,7 +630,7 @@ class serializer if (is_negative) { *buffer_ptr = '-'; - abs_value = static_cast(std::abs(static_cast(x))); + abs_value = static_cast(-x); // account one more byte for the minus sign n_chars = 1 + count_digits(abs_value); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2a32a82963..3c70bcdaf2 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -14264,7 +14264,7 @@ class serializer if (is_negative) { *buffer_ptr = '-'; - abs_value = static_cast(std::abs(static_cast(x))); + abs_value = static_cast(-x); // account one more byte for the minus sign n_chars = 1 + count_digits(abs_value);