diff options
author | Linus Groh <mail@linusgroh.de> | 2022-01-12 20:03:47 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-01-12 21:24:12 +0100 |
commit | 027e4bd439ef76e70596f2e54011d86f13ea8caa (patch) | |
tree | 5d920dfaa3272919d549ffd18706f5f069017699 | |
parent | 323e1e17cf3cdd7486ac138f6156843fc1b7a255 (diff) | |
download | serenity-027e4bd439ef76e70596f2e54011d86f13ea8caa.zip |
LibJS: Fix calculation overflow in parse_temporal_time_zone_string()
As all variables and numeric literals in the expression have an integral
data type, it would evaluate to an int and could easily overflow as
we're multiplying seconds with 10^9.
Introduce a floating point literal into the expression to make it result
in a double.
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.from.js | 41 |
2 files changed, 36 insertions, 10 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index 0848e97139..b8aabf494c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -1670,8 +1670,11 @@ ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject // i. Let nanoseconds be 0. nanoseconds = 0; } + // i. Let offsetNanoseconds be sign × (((hours × 60 + minutes) × 60 + seconds) × 10^9 + nanoseconds). - auto offset_nanoseconds = sign * (((hours * 60 + minutes) * 60 + seconds) * 1000000000 + nanoseconds); + // NOTE: Decimal point in 10^9 is important, otherwise it's all integers and the result overflows! + auto offset_nanoseconds = sign * (((hours * 60 + minutes) * 60 + seconds) * 1000000000.0 + nanoseconds); + // j. Let offsetString be ! FormatTimeZoneOffsetString(offsetNanoseconds). offset = format_time_zone_offset_string(offset_nanoseconds); } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.from.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.from.js index 9fcf5cab9f..04b85097f6 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.from.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.from.js @@ -4,20 +4,43 @@ describe("normal behavior", () => { }); test("basic functionality", () => { + // From object const timeZone = new Temporal.TimeZone("UTC"); const timeZoneLike = {}; const zonedDateTimeLike = { timeZone: {} }; expect(Temporal.TimeZone.from(timeZone)).toBe(timeZone); expect(Temporal.TimeZone.from(timeZoneLike)).toBe(timeZoneLike); expect(Temporal.TimeZone.from(zonedDateTimeLike)).toBe(zonedDateTimeLike.timeZone); - expect(Temporal.TimeZone.from("UTC").id).toBe("UTC"); - expect(Temporal.TimeZone.from("GMT").id).toBe("UTC"); - expect(Temporal.TimeZone.from("Etc/UTC").id).toBe("UTC"); - expect(Temporal.TimeZone.from("Etc/GMT").id).toBe("UTC"); - // FIXME: https://github.com/tc39/proposal-temporal/issues/1993 - // expect(Temporal.TimeZone.from("Etc/GMT+12").id).toBe("Etc/GMT+12"); - // expect(Temporal.TimeZone.from("Etc/GMT-12").id).toBe("Etc/GMT-12"); - expect(Temporal.TimeZone.from("Europe/London").id).toBe("Europe/London"); - expect(Temporal.TimeZone.from("Europe/Isle_of_Man").id).toBe("Europe/London"); + + // From string + const values = [ + ["UTC", "UTC"], + ["GMT", "UTC"], + ["Etc/UTC", "UTC"], + ["Etc/GMT", "UTC"], + // FIXME: https://github.com/tc39/proposal-temporal/issues/1993 + // ["Etc/GMT+12", "Etc/GMT+12"], + // ["Etc/GMT-12", "Etc/GMT-12"], + ["Europe/London", "Europe/London"], + ["Europe/Isle_of_Man", "Europe/London"], + ["1970-01-01+01", "+01:00"], + ["1970-01-01+01[-12:34]", "+01:00"], + ["1970-01-01T00:00:00+01", "+01:00"], + ["1970-01-01T00:00:00.000000000+01", "+01:00"], + ["1970-01-01T00:00:00.000000000+01:00:00", "+01:00"], + ["1970-01-01+12:34", "+12:34"], + ["1970-01-01+12:34:56", "+12:34:56"], + // FIXME: These currently crash :^( + // ["1970-01-01+12:34:56.789", "+12:34:56.789"], + // ["1970-01-01+12:34:56.789[-01:00]", "+12:34:56.789"], + ["1970-01-01-12:34", "-12:34"], + ["1970-01-01-12:34:56", "-12:34:56"], + // FIXME: These currently crash :^( + // ["1970-01-01-12:34:56.789", "-12:34:56.789"], + // ["1970-01-01-12:34:56.789[+01:00]", "-12:34:56.789"], + ]; + for (const [arg, expected] of values) { + expect(Temporal.TimeZone.from(arg).id).toBe(expected); + } }); }); |