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:
Ali Mohammad Pur 2021-05-07 09:45:54 +04:30 committed by Andreas Kling
parent df52040ce9
commit da68c4580c

View file

@ -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;
}