mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-10 10:09:14 +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: da68c4580c
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)
|
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;
|
m_value /= other;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue