Skip to content

Commit 2485664

Browse files
committed
Normative: Prevent indefinite loops in NormalizedTimeDurationToDays
It's possible to make at least the second loop continue indefinitely with a contrived calendar and time zone. DRAFT: Still to be determined if this precludes any non-contrived use cases. If so, we will keep the loops, but still put an upper limit on the number of iterations. Includes a few more tests in the NYSE time zone cookbook example to make sure that a time zone transition of >24h continues to work.
1 parent 89235f9 commit 2485664

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

polyfill/lib/ecmascript.mjs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,20 +3256,34 @@ export function NormalizedTimeDurationToDays(norm, zonedRelativeTo, timeZoneRec,
32563256
// back inside the period where it belongs. Note that this case only can
32573257
// happen for positive durations because the only direction that
32583258
// `disambiguation: 'compatible'` can change clock time is forwards.
3259-
if (sign === 1) {
3260-
while (days > 0 && relativeResult.epochNs.greater(endNs)) {
3261-
days--;
3262-
relativeResult = AddDaysToZonedDateTime(start, dtStart, timeZoneRec, calendar, days);
3263-
// may do disambiguation
3259+
if (sign === 1 && days > 0 && relativeResult.epochNs.greater(endNs)) {
3260+
days--;
3261+
relativeResult = AddDaysToZonedDateTime(start, dtStart, timeZoneRec, calendar, days);
3262+
// may do disambiguation
3263+
if (days > 0 && relativeResult.epochNs.greater(endNs)) {
3264+
throw new RangeError('inconsistent result from custom time zone getInstantFor()');
32643265
}
32653266
}
32663267
norm = TimeDuration.fromEpochNsDiff(endNs, relativeResult.epochNs);
32673268

3268-
let isOverflow = false;
3269-
let dayLengthNs;
3270-
do {
3271-
// calculate length of the next day (day that contains the time remainder)
3272-
const oneDayFarther = AddDaysToZonedDateTime(
3269+
// calculate length of the next day (day that contains the time remainder)
3270+
let oneDayFarther = AddDaysToZonedDateTime(
3271+
relativeResult.instant,
3272+
relativeResult.dateTime,
3273+
timeZoneRec,
3274+
calendar,
3275+
sign
3276+
);
3277+
let dayLengthNs = TimeDuration.fromEpochNsDiff(oneDayFarther.epochNs, relativeResult.epochNs);
3278+
const oneDayLess = norm.subtract(dayLengthNs);
3279+
let isOverflow = oneDayLess.sign() * sign >= 0;
3280+
if (isOverflow) {
3281+
norm = oneDayLess;
3282+
relativeResult = oneDayFarther;
3283+
days += sign;
3284+
3285+
// ensure there was no more overflow
3286+
oneDayFarther = AddDaysToZonedDateTime(
32733287
relativeResult.instant,
32743288
relativeResult.dateTime,
32753289
timeZoneRec,
@@ -3278,14 +3292,9 @@ export function NormalizedTimeDurationToDays(norm, zonedRelativeTo, timeZoneRec,
32783292
);
32793293

32803294
dayLengthNs = TimeDuration.fromEpochNsDiff(oneDayFarther.epochNs, relativeResult.epochNs);
3281-
const oneDayLess = norm.subtract(dayLengthNs);
3282-
isOverflow = oneDayLess.sign() * sign >= 0;
3283-
if (isOverflow) {
3284-
norm = oneDayLess;
3285-
relativeResult = oneDayFarther;
3286-
days += sign;
3287-
}
3288-
} while (isOverflow);
3295+
isOverflow = norm.subtract(dayLengthNs).sign() * sign >= 0;
3296+
if (isOverflow) throw new RangeError('inconsistent result from custom time zone getPossibleInstantsFor()');
3297+
}
32893298
if (days !== 0 && MathSign(days) != sign) {
32903299
throw new RangeError('Time zone or calendar converted nanoseconds into a number of days with the opposite sign');
32913300
}

spec/zoneddatetime.html

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,23 +1444,22 @@ <h1>
14441444
1. Else if _days_ &lt; 0 and _timeSign_ &lt; 0, then
14451445
1. Set _days_ to _days_ + 1.
14461446
1. Let _relativeResult_ be ? AddDaysToZonedDateTime(_startInstant_, _startDateTime_, _timeZoneRec_, _zonedRelativeTo_.[[Calendar]], _days_).
1447-
1. If _sign_ is 1, then
1448-
1. Repeat, while _days_ &gt; 0 and ℝ(_relativeResult_.[[EpochNanoseconds]]) &gt; _endNs_,
1449-
1. Set _days_ to _days_ - 1.
1450-
1. Set _relativeResult_ to ? AddDaysToZonedDateTime(_startInstant_, _startDateTime_, _timeZoneRec_, _zonedRelativeTo_.[[Calendar]], _days_).
1447+
1. If _sign_ = 1, and _days_ &gt; 0, and ℝ(_relativeResult_.[[EpochNanoseconds]]) &gt; _endNs_, then
1448+
1. Set _days_ to _days_ - 1.
1449+
1. Set _relativeResult_ to ? AddDaysToZonedDateTime(_startInstant_, _startDateTime_, _timeZoneRec_, _zonedRelativeTo_.[[Calendar]], _days_).
1450+
1. If _days_ &gt; 0 and ℝ(_relativeResult_.[[EpochNanoseconds]]) &gt; _endNs_, throw a *RangeError* exception.
14511451
1. Set _norm_ to NormalizedTimeDurationFromEpochNanosecondsDifference(_endNs_, _relativeResult_.[[EpochNanoseconds]]).
1452-
1. Let _done_ be *false*.
1453-
1. Let _dayLengthNs_ be ~unset~.
1454-
1. Repeat, while _done_ is *false*,
1455-
1. Let _oneDayFarther_ be ? AddDaysToZonedDateTime(_relativeResult_.[[Instant]], _relativeResult_.[[DateTime]], _timeZoneRec_, _zonedRelativeTo_.[[Calendar]], _sign_).
1456-
1. Set _dayLengthNs_ to NormalizedTimeDurationFromEpochNanosecondsDifference(_oneDayFarther_.[[EpochNanoseconds]], _relativeResult_.[[EpochNanoseconds]]).
1457-
1. Let _oneDayLess_ be ! SubtractNormalizedTimeDuration(_norm_, _dayLengthNs_).
1458-
1. If NormalizedTimeDurationSign(_oneDayLess_) &times; _sign_ &ge; 0, then
1459-
1. Set _norm_ to _oneDayLess_.
1460-
1. Set _relativeResult_ to _oneDayFarther_.
1461-
1. Set _days_ to _days_ + _sign_.
1462-
1. Else,
1463-
1. Set _done_ to *true*.
1452+
1. Let _oneDayFarther_ be ? AddDaysToZonedDateTime(_relativeResult_.[[Instant]], _relativeResult_.[[DateTime]], _timeZoneRec_, _zonedRelativeTo_.[[Calendar]], _sign_).
1453+
1. Let _dayLengthNs_ be NormalizedTimeDurationFromEpochNanosecondsDifference(_oneDayFarther.[[EpochNanoseconds]], _relativeResult_.[[EpochNanoseconds]]).
1454+
1. Let _oneDayLess_ be ! SubtractNormalizedTimeDuration(_norm_, _dayLengthNs_).
1455+
1. If NormalizedTimeDurationSign(_oneDayLess_) &times; _sign_ &ge; 0, then
1456+
1. Set _norm_ to _oneDayLess_.
1457+
1. Set _relativeResult_ to _oneDayFarther_.
1458+
1. Set _days_ to _days_ + _sign_.
1459+
1. Set _oneDayFarther_ to ? AddDaysToZonedDateTime(_relativeResult_.[[Instant]], _relativeResult_.[[DateTime]], _timeZoneRec_, _zonedRelativeTo_.[[Calendar]], _sign_).
1460+
1. Set _dayLengthNs_ to NormalizedTimeDurationFromEpochNanosecondsDifference(_oneDayFarther.[[EpochNanoseconds]], _relativeResult_.[[EpochNanoseconds]]).
1461+
1. If NormalizedTimeDurationSign(? SubtractNormalizedTimeDuration(_norm_, _dayLengthNs_)) &times; _sign_ &ge; 0, then
1462+
1. Throw a *RangeError* exception.
14641463
1. If _days_ &lt; 0 and _sign_ = 1, throw a *RangeError* exception.
14651464
1. If _days_ &gt; 0 and _sign_ = -1, throw a *RangeError* exception.
14661465
1. If NormalizedTimeDurationSign(_norm_) = -1, then

0 commit comments

Comments
 (0)