Added test for negative integral value overflow

Checks for int(-136) which on two's complement should be 'x'
This commit is contained in:
David Xchel Morales Hurtado 2026-07-10 21:55:31 -06:00
parent d34e749f6e
commit a612e331f3
2 changed files with 2 additions and 1 deletions

View File

@ -2127,7 +2127,7 @@ FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg<T> 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<Char>(out, static_cast<Char>(abs_value), specs);
}

View File

@ -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<int>('x')), "x");
EXPECT_EQ(fmt::format("{:c}", int(-136)), "x");
}
TEST(format_test, format_bin) {