From a612e331f3e813ef530dd7bc6c2da54f49995e45 Mon Sep 17 00:00:00 2001 From: David Xchel Morales Hurtado Date: Fri, 10 Jul 2026 21:55:31 -0600 Subject: [PATCH] Added test for negative integral value overflow Checks for int(-136) which on two's complement should be 'x' --- include/fmt/format.h | 2 +- test/format-test.cc | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 29834cfb..70b14c21 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2127,7 +2127,7 @@ FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg arg, // Handles negative cases on casts to smaller types using Two's complement // One example is int{-104} to char which std::format uses as 152 if (sizeof(Char) < sizeof(abs_value) && (arg.prefix & 0xff) == '-') - abs_value = (1 << (8*sizeof(Char))) - abs_value; + abs_value = (1 << (8*sizeof(Char))) - abs_value; return write_char(out, static_cast(abs_value), specs); } diff --git a/test/format-test.cc b/test/format-test.cc index 8c2670a1..e40a5b94 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1334,6 +1334,7 @@ TEST(format_test, format_int) { "invalid format specifier"); check_unknown_types(42, "bBdoxXnLc", "integer"); EXPECT_EQ(fmt::format("{:c}", static_cast('x')), "x"); + EXPECT_EQ(fmt::format("{:c}", int(-136)), "x"); } TEST(format_test, format_bin) {