From 877aea39b0b6fc1dfe76f15b734c1cc87ca37e4d Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 17 Nov 2026 17:19:50 +0100 Subject: [PATCH] Fix narrowing conversions in serializer for small number_magnitude_t Cast the results of the integer promotions in count_digits() and remove_sign() back to number_magnitude_t, which is unsigned char or unsigned short for custom number types. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/output/serializer.hpp | 4 ++-- single_include/nlohmann/json.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index fff89f51c..22fcfa6ac 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -1571,7 +1571,7 @@ class serializer { return n_digits + 3; } - x = x / 10000u; + x = static_cast(x / 10000u); n_digits += 4; } } @@ -2200,7 +2200,7 @@ class serializer number_magnitude_t remove_sign(number_integer_t x) noexcept { JSON_ASSERT(x < 0); - return static_cast(-(x + 1)) + 1; + return static_cast(static_cast(-(x + 1)) + 1); } private: diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ad99819dc..ea6761b78 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -39057,7 +39057,7 @@ class serializer { return n_digits + 3; } - x = x / 10000u; + x = static_cast(x / 10000u); n_digits += 4; } } @@ -39686,7 +39686,7 @@ class serializer number_magnitude_t remove_sign(number_integer_t x) noexcept { JSON_ASSERT(x < 0); - return static_cast(-(x + 1)) + 1; + return static_cast(static_cast(-(x + 1)) + 1); } private: