mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:47:35 +00:00
AK: Make Checked<T> check for division overflow as well
Signed integer overflow can occur with division when the RHS is -1, as the negative values' range is one larger than the positives.
This commit is contained in:
parent
df52040ce9
commit
da68c4580c
1 changed files with 7 additions and 0 deletions
|
@ -183,6 +183,13 @@ public:
|
|||
|
||||
constexpr void div(T other)
|
||||
{
|
||||
if constexpr (IsSigned<T>) {
|
||||
// Ensure that the resulting value won't be out of range, this can only happen when dividing by -1.
|
||||
if (other == -1 && m_value == NumericLimits<T>::min()) {
|
||||
m_overflow = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
m_value /= other;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue