mirror of
https://github.com/fmtlib/fmt.git
synced 2026-08-02 01:37:46 +08:00
Added test for negative integral value overflow
Checks for int(-136) which on two's complement should be 'x'
This commit is contained in:
parent
d34e749f6e
commit
a612e331f3
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user