diff options
author | Nico Weber <thakis@chromium.org> | 2020-08-23 21:37:03 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-24 09:30:11 +0200 |
commit | 84f729c2b4dea4c6abf8aad69928dfe4cf72fb32 (patch) | |
tree | bd64bc573159923af661b2c6f9c657230962eb00 /Libraries | |
parent | 465d46c66542ca96eceb4e1765518ec6b934afec (diff) | |
download | serenity-84f729c2b4dea4c6abf8aad69928dfe4cf72fb32.zip |
LibJS+LibC: Add tests for Date tuple ctor overflow and make mktime()/timegm() handle month overflow
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibC/time.cpp | 8 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/DateConstructor.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/builtins/Date/Date.UTC.js | 20 |
3 files changed, 27 insertions, 3 deletions
diff --git a/Libraries/LibC/time.cpp b/Libraries/LibC/time.cpp index a3fe523de2..018ccf6cda 100644 --- a/Libraries/LibC/time.cpp +++ b/Libraries/LibC/time.cpp @@ -103,6 +103,13 @@ static void time_to_tm(struct tm* tm, time_t t) static time_t tm_to_time(struct tm* tm, long timezone_adjust_seconds) { + tm->tm_year += tm->tm_mon / 12; + tm->tm_mon %= 12; + if (tm->tm_mon < 0) { + tm->tm_year--; + tm->tm_mon += 12; + } + int days = 0; for (int year = 70; year < tm->tm_year; ++year) days += 365 + __is_leap_year(1900 + year); @@ -110,7 +117,6 @@ static time_t tm_to_time(struct tm* tm, long timezone_adjust_seconds) days -= 365 + __is_leap_year(1900 + year); tm->tm_yday = tm->tm_mday - 1; - // FIXME: What if tm->tm_mon < 0 or tm->tm_mon > 12? for (int month = 0; month < tm->tm_mon; ++month) tm->tm_yday += __days_per_month[month]; if (tm->tm_mon > 1 && __is_leap_year(1900 + tm->tm_year)) diff --git a/Libraries/LibJS/Runtime/DateConstructor.cpp b/Libraries/LibJS/Runtime/DateConstructor.cpp index ec91f36897..583230c1b1 100644 --- a/Libraries/LibJS/Runtime/DateConstructor.cpp +++ b/Libraries/LibJS/Runtime/DateConstructor.cpp @@ -179,7 +179,6 @@ Value DateConstructor::construct(Interpreter& interpreter, Function&) value = parse_simplified_iso8601(value.as_string().string()); // A timestamp since the epoch, in UTC. // FIXME: Date() probably should use a double as internal representation, so that NaN arguments and larger offsets are handled correctly. - // FIXME: DateTime::from_timestamp() seems to not support negative offsets. double value_as_double = value.to_double(interpreter); auto datetime = Core::DateTime::from_timestamp(static_cast<time_t>(value_as_double / 1000)); auto milliseconds = static_cast<u16>(fmod(value_as_double, 1000)); @@ -187,7 +186,6 @@ Value DateConstructor::construct(Interpreter& interpreter, Function&) } // A date/time in components, in local time. // FIXME: This doesn't construct an "Invalid Date" object if one of the parameters is NaN. - // FIXME: This doesn't range-check args and convert months > 12 to year increments etc. auto arg_or = [&interpreter](size_t i, i32 fallback) { return interpreter.argument_count() > i ? interpreter.argument(i).to_i32(interpreter) : fallback; }; int year = interpreter.argument(0).to_i32(interpreter); int month_index = interpreter.argument(1).to_i32(interpreter); diff --git a/Libraries/LibJS/Tests/builtins/Date/Date.UTC.js b/Libraries/LibJS/Tests/builtins/Date/Date.UTC.js index 8a5c0e09dc..f1194dd495 100644 --- a/Libraries/LibJS/Tests/builtins/Date/Date.UTC.js +++ b/Libraries/LibJS/Tests/builtins/Date/Date.UTC.js @@ -25,3 +25,23 @@ test("basic functionality", () => { expect(Date.UTC(20000, 0)).toBe(568971820800000); }); + +test("out of range", () => { + expect(Date.UTC(2020, -20)).toBe(1525132800000); + expect(Date.UTC(2020, 20)).toBe(1630454400000); + + expect(Date.UTC(2020, 1, -10)).toBe(1579564800000); + expect(Date.UTC(2020, 1, 40)).toBe(1583884800000); + + expect(Date.UTC(2020, 1, 15, -50)).toBe(1581544800000); + expect(Date.UTC(2020, 1, 15, 50)).toBe(1581904800000); + + expect(Date.UTC(2020, 1, 15, 12, -123)).toBe(1581760620000); + expect(Date.UTC(2020, 1, 15, 12, 123)).toBe(1581775380000); + + expect(Date.UTC(2020, 1, 15, 12, 30, -123)).toBe(1581769677000); + expect(Date.UTC(2020, 1, 15, 12, 30, 123)).toBe(1581769923000); + + expect(Date.UTC(2020, 1, 15, 12, 30, 30, -2345)).toBe(1581769827655); + expect(Date.UTC(2020, 1, 15, 12, 30, 30, 2345)).toBe(1581769832345); +}); |