1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:17:44 +00:00

LibJS/Temporal: Perform floating point arithmetic in RoundTime

The valid range for temporal values (`nsMinInstant`/`nsMaxInstant`)
means performing nanosecond-valued integers could lead to an overflow.

NB: Only the `roundingMode: "day"` case was affected, as all others were
already performing the division on floating-point `fractional_second`
values. I'm adding `.0` suffixes everywhere to make this fact clearer.

This adds a few local tests as well, as those are tested with sanitizers
enabled by default, unlike test262.
This commit is contained in:
Daniel Bertalan 2023-06-29 20:48:52 +02:00 committed by Andreas Kling
parent 1dce1994eb
commit 96b197ef46
2 changed files with 25 additions and 3 deletions

View file

@ -88,6 +88,28 @@ describe("correct behavior", () => {
plainDateTime.round("minute").equals(plainDateTime.round({ smallestUnit: "minute" }))
).toBeTrue();
});
test("range boundary conditions", () => {
// PlainDateTime can represent a point of time ±10**8 days from the epoch.
const min = new Temporal.PlainDateTime(-271821, 4, 19, 0, 0, 0, 0, 0, 1);
const max = new Temporal.PlainDateTime(275760, 9, 13, 23, 59, 59, 999, 999, 999);
["day", "hour", "minute", "second", "millisecond", "microsecond"].forEach(smallestUnit => {
expect(() => {
min.round({ smallestUnit, roundingMode: "floor" });
}).toThrow(RangeError);
expect(() => {
min.round({ smallestUnit, roundingMode: "ceil" });
}).not.toThrow();
expect(() => {
max.round({ smallestUnit, roundingMode: "floor" });
}).not.toThrow();
expect(() => {
max.round({ smallestUnit, roundingMode: "ceil" });
}).toThrow(RangeError);
});
});
});
describe("errors", () => {