mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 19:54:57 +00:00
AK: Make sure no overflow occurs in number_string_with_one_decimal
A possible integer overflow might have occured inside the function in case (number % unit) * 10 did not fit into a u64. So it is verified that this does not happen at the beginning of the function.
This commit is contained in:
parent
76fce0b899
commit
ccb6b4f943
1 changed files with 8 additions and 2 deletions
|
@ -4,8 +4,10 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/Assertions.h>
|
||||||
#include <AK/DeprecatedString.h>
|
#include <AK/DeprecatedString.h>
|
||||||
#include <AK/NumberFormat.h>
|
#include <AK/NumberFormat.h>
|
||||||
|
#include <AK/NumericLimits.h>
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
@ -13,8 +15,12 @@ namespace AK {
|
||||||
// FIXME: Remove this hackery once printf() supports floats.
|
// FIXME: Remove this hackery once printf() supports floats.
|
||||||
static DeprecatedString number_string_with_one_decimal(u64 number, u64 unit, StringView suffix)
|
static DeprecatedString number_string_with_one_decimal(u64 number, u64 unit, StringView suffix)
|
||||||
{
|
{
|
||||||
int decimal = (number % unit) * 10 / unit;
|
constexpr auto max_unit_size = NumericLimits<u64>::max() / 10;
|
||||||
return DeprecatedString::formatted("{}.{} {}", number / unit, decimal, suffix);
|
VERIFY(unit < max_unit_size);
|
||||||
|
|
||||||
|
auto integer_part = number / unit;
|
||||||
|
auto decimal_part = (number % unit) * 10 / unit;
|
||||||
|
return DeprecatedString::formatted("{}.{} {}", integer_part, decimal_part, suffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
DeprecatedString human_readable_quantity(u64 quantity, StringView unit)
|
DeprecatedString human_readable_quantity(u64 quantity, StringView unit)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue