1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:24:57 +00:00

AK: Avoid overflow when passing i64 minimum value to vformat()

This commit is contained in:
Tim Ledbetter 2023-10-08 17:53:10 +01:00 committed by Ali Mohammad Pur
parent 2ea45f4881
commit f10db48bab
2 changed files with 9 additions and 2 deletions

View file

@ -363,9 +363,14 @@ ErrorOr<void> FormatBuilder::put_i64(
SignMode sign_mode)
{
auto const is_negative = value < 0;
value = is_negative ? -value : value;
u64 positive_value;
if (value == NumericLimits<i64>::min()) {
positive_value = static_cast<u64>(NumericLimits<i64>::max()) + 1;
} else {
positive_value = is_negative ? -value : value;
}
TRY(put_u64(static_cast<u64>(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 {};
}