From 59162c815515986eb92b473bf346405c9ee34815 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 16 Jan 2025 08:46:49 -0500 Subject: [PATCH] LibJS: Adjust ad-hoc clamping behavior in RegulateISODate Instead of clamping to the limits allowed by ISOYearMonthWithinLimits, clamp to the limits allowed by the type we are converting to (i32). This allows some callers to then reject years outside that range. --- Libraries/LibJS/Runtime/Temporal/PlainDate.cpp | 10 ++++++---- .../Temporal/Duration/Duration.prototype.total.js | 9 +++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp b/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp index 6515aafbcff..4a8ad40d483 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp +++ b/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp @@ -2,12 +2,13 @@ * Copyright (c) 2021, Idan Horowitz * Copyright (c) 2021-2023, Linus Groh * Copyright (c) 2024, Shannon Booth - * Copyright (c) 2024, Tim Flynn + * Copyright (c) 2024-2025, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #include +#include #include #include #include @@ -202,9 +203,10 @@ ThrowCompletionOr regulate_iso_date(VM& vm, double year, double month, // c. Set day to the result of clamping day between 1 and daysInMonth. day = clamp(day, 1, iso_days_in_month(year, month)); - // AD-HOC: We further clamp the year to the range allowed by ISOYearMonthWithinLimits, to ensure we do not - // overflow when we store the year as an integer. - year = clamp(year, -271821, 275760); + // AD-HOC: We further clamp the year to the range allowed by ISODate.year, to ensure we do not overflow when we + // store the year as an integer. + using YearType = decltype(declval().year); + year = clamp(year, static_cast(NumericLimits::min()), static_cast(NumericLimits::max())); break; diff --git a/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.total.js b/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.total.js index 32ed14c9e1a..298e379fd82 100644 --- a/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.total.js +++ b/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.total.js @@ -87,10 +87,15 @@ describe("errors", () => { }); test("relativeTo with invalid date", () => { - const duration = new Temporal.Duration(0, 0, 0, 31); - expect(() => { + const duration = new Temporal.Duration(0, 0, 0, 31); duration.total({ unit: "minute", relativeTo: "-271821-04-19" }); }).toThrowWithMessage(RangeError, "Invalid ISO date time"); + + expect(() => { + const duration = new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 1); + const relativeTo = new Temporal.ZonedDateTime(864n * 10n ** 19n - 1n, "UTC"); + duration.total({ unit: "years", relativeTo: relativeTo }); + }).toThrowWithMessage(RangeError, "Invalid ISO date"); }); });