tor-browser

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

commit 04f2aead1f616870c263d5043eb728f5a6f1c4d9
parent bd837e0ebcbe262053516e35f43da359e0d7b7c3
Author: Serban Stanca <sstanca@mozilla.com>
Date:   Wed,  8 Oct 2025 19:45:12 +0300

Revert "Bug 1993171 - Cleanup mfbt/MathAlgorithms.h r=emilio,win-reviewers,layout-reviewers,gstoll" for causing spidermonkey bustages in TestCeilingFloor.cpp.

This reverts commit aad24563c3bbf81d2e90b03c568a2da03a6f0491.

Diffstat:
Mdocshell/shistory/nsSHistory.cpp | 14++++++--------
Mdom/events/EventStateManager.cpp | 31++++++++++++++-----------------
Meditor/libeditor/HTMLEditorObjectResizer.cpp | 6++----
Mlayout/tables/nsTableFrame.cpp | 8++++----
Mmemory/build/Utils.h | 2+-
Mmfbt/MathAlgorithms.h | 70+++++++++++++++++++++++++++++++++++++++++++++++-----------------------
Mmozglue/misc/TimeStamp_windows.cpp | 7+++----
Mwidget/windows/nsWindow.cpp | 21++++++++-------------
Mxpcom/io/nsMultiplexInputStream.cpp | 5+++--
9 files changed, 88 insertions(+), 76 deletions(-)

diff --git a/docshell/shistory/nsSHistory.cpp b/docshell/shistory/nsSHistory.cpp @@ -1862,9 +1862,6 @@ void nsSHistory::GloballyEvictDocumentViewers() { nsCOMPtr<nsISHEntry> entry = shist->mEntries[i]; nsCOMPtr<nsIDocumentViewer> viewer = entry->GetDocumentViewer(); - MOZ_ASSERT(mozilla::IsValidAbsArgument(i - shist->mIndex)); - const int32_t idiff = std::abs(i - shist->mIndex); - bool found = false; bool hasDocumentViewerOrFrameLoader = false; if (viewer) { @@ -1876,7 +1873,8 @@ void nsSHistory::GloballyEvictDocumentViewers() { for (uint32_t j = 0; j < shEntries.Length(); j++) { EntryAndDistance& container = shEntries[j]; if (container.mViewer == viewer) { - container.mDistance = std::min(container.mDistance, idiff); + container.mDistance = + std::min(container.mDistance, DeprecatedAbs(i - shist->mIndex)); found = true; break; } @@ -1888,8 +1886,8 @@ void nsSHistory::GloballyEvictDocumentViewers() { for (uint32_t j = 0; j < shEntries.Length(); j++) { EntryAndDistance& container = shEntries[j]; if (container.mFrameLoader == frameLoader) { - container.mDistance = std::min(container.mDistance, idiff); - ; + container.mDistance = std::min(container.mDistance, + DeprecatedAbs(i - shist->mIndex)); found = true; break; } @@ -1900,8 +1898,8 @@ void nsSHistory::GloballyEvictDocumentViewers() { // If we didn't find a EntryAndDistance for this content viewer / // frameloader, make a new one. if (hasDocumentViewerOrFrameLoader && !found) { - MOZ_ASSERT(mozilla::IsValidAbsArgument(i - shist->mIndex)); - EntryAndDistance container(shist, entry, std::abs(i - shist->mIndex)); + EntryAndDistance container(shist, entry, + DeprecatedAbs(i - shist->mIndex)); shEntries.AppendElement(container); } } diff --git a/dom/events/EventStateManager.cpp b/dom/events/EventStateManager.cpp @@ -3647,23 +3647,20 @@ void EventStateManager::DoScrollText( nsSize pageSize = aScrollContainerFrame->GetPageScrollAmount(); nsIntSize devPixelPageSize(pc->AppUnitsToDevPixels(pageSize.width), pc->AppUnitsToDevPixels(pageSize.height)); - if (!WheelPrefs::GetInstance()->IsOverOnePageScrollAllowedX(aEvent)) { - MOZ_ASSERT(mozilla::IsValidAbsArgument(actualDevPixelScrollAmount.x.value)); - if (std::abs(actualDevPixelScrollAmount.x.value) > devPixelPageSize.width) { - actualDevPixelScrollAmount.x = (actualDevPixelScrollAmount.x >= 0) - ? devPixelPageSize.width - : -devPixelPageSize.width; - } - } - - if (!WheelPrefs::GetInstance()->IsOverOnePageScrollAllowedY(aEvent)) { - MOZ_ASSERT(mozilla::IsValidAbsArgument(actualDevPixelScrollAmount.y.value)); - if (std::abs(actualDevPixelScrollAmount.y.value) > - devPixelPageSize.height) { - actualDevPixelScrollAmount.y = (actualDevPixelScrollAmount.y >= 0) - ? devPixelPageSize.height - : -devPixelPageSize.height; - } + if (!WheelPrefs::GetInstance()->IsOverOnePageScrollAllowedX(aEvent) && + DeprecatedAbs(actualDevPixelScrollAmount.x.value) > + devPixelPageSize.width) { + actualDevPixelScrollAmount.x = (actualDevPixelScrollAmount.x >= 0) + ? devPixelPageSize.width + : -devPixelPageSize.width; + } + + if (!WheelPrefs::GetInstance()->IsOverOnePageScrollAllowedY(aEvent) && + DeprecatedAbs(actualDevPixelScrollAmount.y.value) > + devPixelPageSize.height) { + actualDevPixelScrollAmount.y = (actualDevPixelScrollAmount.y >= 0) + ? devPixelPageSize.height + : -devPixelPageSize.height; } bool isDeltaModePixel = diff --git a/editor/libeditor/HTMLEditorObjectResizer.cpp b/editor/libeditor/HTMLEditorObjectResizer.cpp @@ -1163,10 +1163,8 @@ nsresult HTMLEditor::UpdateResizerOrGrabberPositionTo( int32_t yThreshold = LookAndFeel::GetInt(LookAndFeel::IntID::DragThresholdY, 1); - MOZ_ASSERT(mozilla::IsValidAbsArgument(aClientPoint.x - mOriginalX)); - MOZ_ASSERT(mozilla::IsValidAbsArgument(aClientPoint.y - mOriginalY)); - if (std::abs(aClientPoint.x - mOriginalX) * 2 >= xThreshold || - std::abs(aClientPoint.y - mOriginalY) * 2 >= yThreshold) { + if (DeprecatedAbs(aClientPoint.x - mOriginalX) * 2 >= xThreshold || + DeprecatedAbs(aClientPoint.y - mOriginalY) * 2 >= yThreshold) { mGrabberClicked = false; DebugOnly<nsresult> rvIgnored = StartMoving(); NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored), diff --git a/layout/tables/nsTableFrame.cpp b/layout/tables/nsTableFrame.cpp @@ -6132,10 +6132,10 @@ bool BCPaintBorderIterator::SetDamageArea(const nsRect& aDirtyRect) { if (!haveIntersect) { return false; } - MOZ_ASSERT(mozilla::IsValidAbsArgument(int32_t(endColIndex - startColIndex))); - mDamageArea = TableArea(startColIndex, startRowIndex, - 1 + std::abs(int32_t(endColIndex - startColIndex)), - 1 + endRowIndex - startRowIndex); + mDamageArea = + TableArea(startColIndex, startRowIndex, + 1 + DeprecatedAbs<int32_t>(endColIndex - startColIndex), + 1 + endRowIndex - startRowIndex); Reset(); mBlockDirInfo = MakeUnique<BCBlockDirSeg[]>(mDamageArea.ColCount() + 1); diff --git a/memory/build/Utils.h b/memory/build/Utils.h @@ -161,7 +161,7 @@ class FastDivisor { unsigned m_ = ((1U << p) + div - 1 - (((1U << p) - 1) % div)) / div; // Make sure that max * m does not overflow. - MOZ_DIAGNOSTIC_ASSERT(max < std::numeric_limits<unsigned int>::max() / m_); + MOZ_DIAGNOSTIC_ASSERT(max < UINT_MAX / m_); MOZ_ASSERT(m_ <= std::numeric_limits<T>::max()); m = static_cast<T>(m_); diff --git a/mfbt/MathAlgorithms.h b/mfbt/MathAlgorithms.h @@ -13,7 +13,7 @@ #include <algorithm> #include <cmath> -#include <limits> +#include <climits> #include <cstdint> #include <type_traits> @@ -21,6 +21,46 @@ namespace mozilla { namespace detail { +template <typename T> +struct AllowDeprecatedAbsFixed : std::false_type {}; + +template <> +struct AllowDeprecatedAbsFixed<int32_t> : std::true_type {}; +template <> +struct AllowDeprecatedAbsFixed<int64_t> : std::true_type {}; + +template <typename T> +struct AllowDeprecatedAbs : AllowDeprecatedAbsFixed<T> {}; + +template <> +struct AllowDeprecatedAbs<int> : std::true_type {}; +template <> +struct AllowDeprecatedAbs<long> : std::true_type {}; + +} // namespace detail + +// DO NOT USE DeprecatedAbs. It exists only until its callers can be converted +// to Abs below, and it will be removed when all callers have been changed. +template <typename T> +inline std::enable_if_t<detail::AllowDeprecatedAbs<T>::value, T> DeprecatedAbs( + const T aValue) { + // The absolute value of the smallest possible value of a signed-integer type + // won't fit in that type (on twos-complement systems -- and we're blithely + // assuming we're on such systems, for the non-<stdint.h> types listed above), + // so assert that the input isn't that value. + // + // This is the case if: the value is non-negative; or if adding one (giving a + // value in the range [-maxvalue, 0]), then negating (giving a value in the + // range [0, maxvalue]), doesn't produce maxvalue (because in twos-complement, + // (minvalue + 1) == -maxvalue). + MOZ_ASSERT(aValue >= 0 || + -(aValue + 1) != T((1ULL << (CHAR_BIT * sizeof(T) - 1)) - 1), + "You can't negate the smallest possible negative integer!"); + return aValue >= 0 ? aValue : -aValue; +} + +namespace detail { + template <typename T, typename = void> struct AbsReturnType; @@ -37,13 +77,6 @@ struct AbsReturnType<T, std::enable_if_t<std::is_floating_point_v<T>>> { } // namespace detail -template <class T> -constexpr bool IsValidAbsArgument(T val) { - static_assert(std::is_integral_v<T> && std::is_signed_v<T>, - "no need to validate unsigned or floating point type"); - return val != std::numeric_limits<T>::min(); -} - template <typename T> inline constexpr typename detail::AbsReturnType<T>::Type Abs(const T aValue) { using ReturnType = typename detail::AbsReturnType<T>::Type; @@ -261,11 +294,8 @@ constexpr uint_fast8_t FloorLog2Size(size_t aValue) { * be so great that the computed value would overflow |size_t|. */ constexpr size_t RoundUpPow2(size_t aValue) { - MOZ_ASSERT( - aValue <= (size_t(1) << (sizeof(size_t) * - std::numeric_limits<unsigned char>::digits - - 1)), - "can't round up -- will overflow!"); + MOZ_ASSERT(aValue <= (size_t(1) << (sizeof(size_t) * CHAR_BIT - 1)), + "can't round up -- will overflow!"); return size_t(1) << CeilingLog2(aValue); } @@ -277,17 +307,14 @@ MOZ_NO_SANITIZE_UNSIGNED_OVERFLOW constexpr T RotateLeft(const T aValue, uint_fast8_t aShift) { static_assert(std::is_unsigned_v<T>, "Rotates require unsigned values"); - MOZ_ASSERT(aShift < sizeof(T) * std::numeric_limits<unsigned char>::digits, - "Shift value is too large!"); + MOZ_ASSERT(aShift < sizeof(T) * CHAR_BIT, "Shift value is too large!"); MOZ_ASSERT(aShift > 0, "Rotation by value length is undefined behavior, but compilers " "do not currently fold a test into the rotate instruction. " "Please remove this restriction when compilers optimize the " "zero case (http://blog.regehr.org/archives/1063)."); - return (aValue << aShift) | - (aValue >> - (sizeof(T) * std::numeric_limits<unsigned char>::digits - aShift)); + return (aValue << aShift) | (aValue >> (sizeof(T) * CHAR_BIT - aShift)); } /** @@ -298,17 +325,14 @@ MOZ_NO_SANITIZE_UNSIGNED_OVERFLOW constexpr T RotateRight(const T aValue, uint_fast8_t aShift) { static_assert(std::is_unsigned_v<T>, "Rotates require unsigned values"); - MOZ_ASSERT(aShift < sizeof(T) * std::numeric_limits<unsigned char>::digits, - "Shift value is too large!"); + MOZ_ASSERT(aShift < sizeof(T) * CHAR_BIT, "Shift value is too large!"); MOZ_ASSERT(aShift > 0, "Rotation by value length is undefined behavior, but compilers " "do not currently fold a test into the rotate instruction. " "Please remove this restriction when compilers optimize the " "zero case (http://blog.regehr.org/archives/1063)."); - return (aValue >> aShift) | - (aValue << (sizeof(T) * std::numeric_limits<unsigned char>::digits - - aShift)); + return (aValue >> aShift) | (aValue << (sizeof(T) * CHAR_BIT - aShift)); } /** diff --git a/mozglue/misc/TimeStamp_windows.cpp b/mozglue/misc/TimeStamp_windows.cpp @@ -281,14 +281,13 @@ MFBT_API uint64_t TimeStampValue::CheckQPC(const TimeStampValue& aOther) const { } // Check QPC is sane before using it. - uint64_t diff = Abs(deltaQPC - deltaGTC); - MOZ_ASSERT(sGTCResolutionThreshold >= 0); - if (diff <= (uint64_t)sGTCResolutionThreshold) { + int64_t diff = DeprecatedAbs(int64_t(deltaQPC) - int64_t(deltaGTC)); + if (diff <= sGTCResolutionThreshold) { return deltaQPC; } // Treat absolutely for calibration purposes - uint64_t duration = Abs(deltaGTC); + int64_t duration = DeprecatedAbs(int64_t(deltaGTC)); int64_t overflow = diff - sGTCResolutionThreshold; LOG(("TimeStamp: QPC check after %llums with overflow %1.4fms", diff --git a/widget/windows/nsWindow.cpp b/widget/windows/nsWindow.cpp @@ -4314,13 +4314,10 @@ bool nsWindow::DispatchMouseEvent(EventMessage aEventMessage, WPARAM wParam, static LONG sLastClickCount = 0L; static BYTE sLastMouseButton = 0; - static_assert(std::is_same_v < decltype(sLastMousePoint.x), - decltype(eventPoint.x.value)); - MOZ_ASSERT(IsValidAbsArgument(sLastMousePoint.x - eventPoint.x.value)); bool insideMovementThreshold = - (std::abs(sLastMousePoint.x - eventPoint.x.value) < + (DeprecatedAbs(sLastMousePoint.x - eventPoint.x.value) < (short)::GetSystemMetrics(SM_CXDOUBLECLK)) && - (std::abs(sLastMousePoint.y - eventPoint.y.value) < + (DeprecatedAbs(sLastMousePoint.y - eventPoint.y.value) < (short)::GetSystemMetrics(SM_CYDOUBLECLK)); BYTE eventButton; @@ -6864,14 +6861,12 @@ bool nsWindow::OnGesture(WPARAM wParam, LPARAM lParam) { } if (mDisplayPanFeedback) { - const auto RoundedDeltaX = RoundDown(wheelEvent.mOverflowDeltaX); - MOZ_ASSERT(mozilla : IsValidAbsArgument(RoundedDeltaX)); - mGesture.UpdatePanFeedbackX(mWnd, std::abs(RoundedDeltaX), endFeedback); - - const auto RoundedDeltaY = RoundDown(wheelEvent.mOverflowDeltaY); - MOZ_ASSERT(mozilla::IsValidAbsArgument(RoundedDeltaY); - mGesture.UpdatePanFeedbackY(mWnd, std::abs(RoundDeltaY), endFeedback); - + mGesture.UpdatePanFeedbackX( + mWnd, DeprecatedAbs(RoundDown(wheelEvent.mOverflowDeltaX)), + endFeedback); + mGesture.UpdatePanFeedbackY( + mWnd, DeprecatedAbs(RoundDown(wheelEvent.mOverflowDeltaY)), + endFeedback); mGesture.PanFeedbackFinalize(mWnd, endFeedback); } diff --git a/xpcom/io/nsMultiplexInputStream.cpp b/xpcom/io/nsMultiplexInputStream.cpp @@ -34,6 +34,8 @@ using namespace mozilla; using namespace mozilla::ipc; +using mozilla::DeprecatedAbs; + NS_IMPL_ADDREF(nsMultiplexInputStream) NS_IMPL_RELEASE(nsMultiplexInputStream) @@ -557,10 +559,9 @@ nsMultiplexInputStream::Seek(int32_t aWhence, int64_t aOffset) { } int64_t streamLength = avail + mStreams[i].mCurrentPos; - MOZ_ASSERT(mozilla::IsValidAbsArgument(remaining)); // The seek(END) can be completed in the current stream. - if (streamLength >= std::abs(remaining)) { + if (streamLength >= DeprecatedAbs(remaining)) { rv = stream->Seek(NS_SEEK_END, remaining); if (NS_WARN_IF(NS_FAILED(rv))) { return rv;