Bug 1975578 - shift wrappedTimeStampDelta in order to handle negative value. a=RyanVM

The delta of two time stamps can be negative. However, in order to
support 32-bits unsigned time from X server, the delta has been casted
to an unsigned value, causing the failure with negative values.

We translate the origin (value 0) to the half way of the valid range
of Time type. Use this value to compute timestamp and translate back
later so that we can handle delta in the range [MAX/2 + 1, -MAX/2].

For example, if Time is a uint32_t. Delta of value 0 will be
translated to 0x7fffffff.  Delta of value -15 will be transalted to
0x7ffffff0.

Original Revision: https://phabricator.services.mozilla.com/D257794

Differential Revision: https://phabricator.services.mozilla.com/D258760
This commit is contained in:
Kui-Feng Lee
2025-07-27 00:19:04 +00:00
committed by rvandermeulen@mozilla.com
parent 520c64e4d9
commit e777d12072

View File

@@ -200,9 +200,17 @@ class SystemTimeConverter {
TimeDuration timeStampDelta = (aTimeStamp - mReferenceTimeStamp);
int64_t wholeMillis = static_cast<int64_t>(timeStampDelta.ToMilliseconds());
Time wrappedTimeStampDelta = wholeMillis; // truncate to unsigned
// Half of the valid range of Time
const Time shift = (static_cast<Time>(0) - static_cast<Time>(1)) / 2;
// Shift/move origin (0) of the value by UINT32_MAX / 2 and shift
// it back later in order to support negative deltas. With this
// approach we can support deltas before shifting in the range
// [UINT32_MAX / 2 + 1, -UINT32_MAX / 2].
Time wrappedTimeStampDeltaShifted = wrappedTimeStampDelta + shift;
int64_t timeToTimeStamp = static_cast<int64_t>(wrappedTimeStampDelta) -
static_cast<int64_t>(timeDelta);
int64_t timeToTimeStamp = static_cast<int64_t>(wrappedTimeStampDeltaShifted) -
static_cast<int64_t>(timeDelta) -
static_cast<int64_t>(shift);
bool isNewer = false;
if (timeToTimeStamp == 0) {
// wholeMillis needs no adjustment