Quellcode durchsuchen

LibJS: Fix fraction substring range in parse_temporal_time_zone_string()

Two issues:

- The intended range was 9 characters starting from index 1. Since the
  second argument to String::substring() is the length, 10 is
  potentially reading further than the string's length (when only
  providing one fraction digit), causing an assertion failure crash.
- The spec's intention to skip the decimal separator by starting at
  index 1 is incorrect, no decimal separator is present in the result of
  parsing TimeZoneUTCOffsetFractionalPart. I filed a spec fix for this,
  see: https://github.com/tc39/proposal-temporal/pull/1999
Linus Groh vor 3 Jahren
Ursprung
Commit
392f5bfebd

+ 3 - 1
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp

@@ -1663,7 +1663,9 @@ ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject
             auto fraction = String::formatted("{}000000000", *fraction_part);
             // ii. Let nanoseconds be the String value equal to the substring of fraction from 1 to 10.
             // iii. Set nanoseconds to ! ToIntegerOrInfinity(nanoseconds).
-            nanoseconds = *fraction.substring(1, 10).to_int<i32>();
+            // FIXME: 1-10 is wrong and should be 0-9; the decimal separator is no longer present in the parsed string.
+            //        See: https://github.com/tc39/proposal-temporal/pull/1999
+            nanoseconds = *fraction.substring(0, 9).to_int<i32>();
         }
         // h. Else,
         else {

+ 4 - 6
Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.from.js

@@ -30,14 +30,12 @@ describe("normal behavior", () => {
             ["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: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"],
+            ["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);