mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +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
Notes:
sideshowbarker
2024-07-18 18:37:25 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/da68c4580cb Pull-request: https://github.com/SerenityOS/serenity/pull/6908
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…
Reference in a new issue