Repository navigation
Conversation
parseISODate passed every field straight to makeDay/makeTime, so an
out-of-range month, day, hour, minute, second or UTC offset rolled over
instead of making the string invalid: new Date('2024-13-08T00:00:00Z')
was 2025-01-08 and new Date('1999-00-00') was 1998-11-30. The Date Time
String Format says a string with out-of-bounds elements is not a valid
instance of the format, and V8 returns NaN for these.
Check month 1-12, day 1-31, hour 0-24 (24 only as 24:00:00.000), minute
and second 0-59, and offset hours 0-23 and minutes 0-59. As in V8, the
day is not checked against the length of the month, so 2020-02-30 still
rolls over into March.
Fixes meta#1148
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1148 (and its closed duplicate #1291).
Date.parseandnew Date(string)accept an ISO date-time string with an out-of-range field and roll it over instead of returning NaN:new Date('2024-13-08T00:00:00Z')new Date('1999-00-00')Date.parse('2020-01-32')Date.parse('2020-01-01T25:00Z')Date.parse('2020-01-01T23:59:60Z')Date.parse('2020-01-01T00:00+25:00')parseISODatereads every field withscanIntand passes it straight tomakeDay/makeTime, which carry any excess into the next unit. The Date Time String Format section (ES2024 21.4.1.32) says "A string containing out-of-bounds or nonconforming elements is not a valid instance of this format", and the earlier comment on #1148 said that the V8-style validation is what the spec seems to require.This change adds three range checks in
parseISODate, in the existing early-return style:24:00:00.000, right after the time part (before the local-time return, so strings without an offset are covered too);When
parseISODatereturns NaN,parseDatestill triesparseESDate. None of the inputs above parse there, including the space-separated form ('2020-01-01 25:00'), so the result stays NaN.The day is only checked against 31, not against the length of the month, so
2020-02-30still rolls over to March 1. This matches V8. The #1148 comment noted that matching JSC and SpiderMonkey, which reject it, would take more work, so I left that out. Other ISO-format differences I saw (single-digit fields such as2020-1-01, the-000000year) are separate and not changed here.Behaviour that changes besides the rollover cases: a leap second (
:60), hour 24 with a non-zero remainder, and offsets of 24:00 or more now give NaN, as in V8.How I found it
I was running the same set of built-in calls through the
hermesCLI and node v24.14.1 and diffing the output.Date.parse('2020-13-01')printed a time in 2021 under hermes and NaN under node. ReadingparseISODateinlib/VM/JSLib/DateUtil.cppshowed there is no range check on any field. Then I found #1148, which reports the same thing for1999-00-00.Where this can show up in practice
This is my estimate; I have no usage data. React Native apps often check a date that a user typed or that came from an API with
isNaN(new Date(s).getTime())orisNaN(Date.parse(s)). On Hermes, a string like2024-13-08passes that check and silently becomes a date in the next year, while the same code on JSC or in a browser rejects it. #1148 and #1291 are two reports of this from React Native users.Test Plan
test/hermes/date-parse-field-range.js(same RUN lines asdate-parse-time-clip.js): each out-of-range field gives NaN, including the strings from new Date() not throwing an Invalid Date error #1148 and Date constructor accept wrong ISO 8601 date #1291, a local-time string, a space-separated string and a negative offset. The largest valid value of each field still parses, and2020-02-30still rolls over. I checked every expected value against node v24.14.1 withTZ=MST+7.hermes-liton all oftest/hermes(thecheck-hermescommand, pointed at that directory): 663 passed, 3 expected failures, 45 unsupported, 0 failed. The new test passes on both RUN lines (hermes -Oandshermes -exec).:60seconds case, which now prints NaN like node.clang-formatreports no changes forDateUtil.cpp.static_h@ e5d8795.