tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 377cce1674e539f73fc3af5db471f5e3e017b248
parent 4270e3934b8effc3381d3d2fbc3ebdd99d3335fb
Author: Brad Werth <werth@efn.org>
Date:   Tue, 16 Dec 2025 14:52:45 +0000

Bug 2002894 - Part 1: Make BaseRect::XMost() and ::YMost() use saturating addition. r=gfx-reviewers,lsalzman

This ensures that XMost and YMost won't overflow with very wide or high rects.
It only affects integral types that are capable of overflow. Static template
constexpr makes this change a no-op for float types.

One effect of this change is to rationalize internal callers such as
Contains() and Intersects() which will now return more sensible results for
very large rects.

There are many other methods not touched by this patch that may return less
sensible  results with very large rects or large rects passed as params. These
might be fixable with similar application of SaturateOp, but that is not
attempted here.

Differential Revision: https://phabricator.services.mozilla.com/D275163

Diffstat:
Mgfx/2d/BaseRect.h | 18++++++++++++++++--
Mmfbt/Saturate.h | 2++
Mmfbt/api.yml | 1+
3 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/gfx/2d/BaseRect.h b/gfx/2d/BaseRect.h @@ -13,6 +13,7 @@ #include <type_traits> #include "mozilla/Assertions.h" +#include "mozilla/Saturate.h" #include "mozilla/gfx/ScaleFactors2D.h" #include "Types.h" @@ -498,8 +499,21 @@ struct BaseRect { MOZ_ALWAYS_INLINE T Y() const { return y; } MOZ_ALWAYS_INLINE T Width() const { return width; } MOZ_ALWAYS_INLINE T Height() const { return height; } - MOZ_ALWAYS_INLINE T XMost() const { return x + width; } - MOZ_ALWAYS_INLINE T YMost() const { return y + height; } + + MOZ_ALWAYS_INLINE T XMost() const { + if constexpr (std::is_integral<T>::value) { + return (Saturate<T>(x) + width).value(); + } else { + return x + width; + } + } + MOZ_ALWAYS_INLINE T YMost() const { + if constexpr (std::is_integral<T>::value) { + return (Saturate<T>(y) + height).value(); + } else { + return y + height; + } + } // Set width and height. SizeTo() sets them together. MOZ_ALWAYS_INLINE void SetWidth(T aWidth) { width = aWidth; } diff --git a/mfbt/Saturate.h b/mfbt/Saturate.h @@ -229,6 +229,8 @@ typedef detail::Saturate<uint32_t> SaturateUint32; typedef detail::Saturate<intptr_t> SaturateIntPtr; typedef detail::Saturate<uintptr_t> SaturateUintPtr; +using detail::Saturate; + } // namespace mozilla template <typename LhsT, typename RhsT> diff --git a/mfbt/api.yml b/mfbt/api.yml @@ -717,6 +717,7 @@ RollingMean.h: Saturate.h: types: + - Saturate - SaturateInt8 - SaturateInt16 - SaturateInt32