diff --git a/AK/Format.cpp b/AK/Format.cpp index 9d2e340921..9206428204 100644 --- a/AK/Format.cpp +++ b/AK/Format.cpp @@ -363,9 +363,14 @@ ErrorOr FormatBuilder::put_i64( SignMode sign_mode) { auto const is_negative = value < 0; - value = is_negative ? -value : value; + u64 positive_value; + if (value == NumericLimits::min()) { + positive_value = static_cast(NumericLimits::max()) + 1; + } else { + positive_value = is_negative ? -value : value; + } - TRY(put_u64(static_cast(value), base, prefix, upper_case, zero_pad, use_separator, align, min_width, fill, sign_mode, is_negative)); + TRY(put_u64(positive_value, base, prefix, upper_case, zero_pad, use_separator, align, min_width, fill, sign_mode, is_negative)); return {}; } diff --git a/Tests/AK/TestFormat.cpp b/Tests/AK/TestFormat.cpp index 73b83dd87f..4602f342ad 100644 --- a/Tests/AK/TestFormat.cpp +++ b/Tests/AK/TestFormat.cpp @@ -35,6 +35,8 @@ TEST_CASE(format_integers) EXPECT_EQ(DeprecatedString::formatted("{:08x}", 4096), "00001000"); EXPECT_EQ(DeprecatedString::formatted("{:x}", 0x1111222233334444ull), "1111222233334444"); EXPECT_EQ(DeprecatedString::formatted("{:4}", 12345678), "12345678"); + EXPECT_EQ(DeprecatedString::formatted("{}", AK::NumericLimits::min()), "-9223372036854775808"); + EXPECT_EQ(DeprecatedString::formatted("{:x}", AK::NumericLimits::min()), "-8000000000000000"); EXPECT_EQ(DeprecatedString::formatted("{:'}", 0), "0"); EXPECT_EQ(DeprecatedString::formatted("{:'}", 4096), "4,096"); EXPECT_EQ(DeprecatedString::formatted("{:'}", 16777216), "16,777,216");