diff --git a/Userland/Libraries/LibWeb/PixelUnits.cpp b/Userland/Libraries/LibWeb/PixelUnits.cpp index 4e766046c2..8edc6e6bec 100644 --- a/Userland/Libraries/LibWeb/PixelUnits.cpp +++ b/Userland/Libraries/LibWeb/PixelUnits.cpp @@ -66,12 +66,12 @@ bool CSSPixels::operator==(CSSPixels const& other) const CSSPixels& CSSPixels::operator++() { - m_value += fixed_point_denominator; + m_value = Checked::saturating_add(m_value, fixed_point_denominator); return *this; } CSSPixels& CSSPixels::operator--() { - m_value -= fixed_point_denominator; + m_value = Checked::saturating_sub(m_value, fixed_point_denominator); return *this; } @@ -91,25 +91,14 @@ CSSPixels CSSPixels::operator-() const return from_raw(-raw_value()); } -static inline int saturated_addition(int a, int b) -{ - i32 overflow = (b > 0 && a > NumericLimits::max() - b); - i32 underflow = (b < 0 && a < NumericLimits::min() - b); - return overflow ? NumericLimits::max() : (underflow ? NumericLimits::min() : a + b); -} - CSSPixels CSSPixels::operator+(CSSPixels const& other) const { - CSSPixels result; - result.set_raw_value(saturated_addition(raw_value(), other.raw_value())); - return result; + return from_raw(Checked::saturating_add(raw_value(), other.raw_value())); } CSSPixels CSSPixels::operator-(CSSPixels const& other) const { - CSSPixels result; - result.set_raw_value(saturated_addition(raw_value(), -other.raw_value())); - return result; + return from_raw(Checked::saturating_sub(raw_value(), other.raw_value())); } CSSPixels CSSPixels::operator*(CSSPixels const& other) const