From 42543bcf478debf7a10f1f291e227ad57b0a38b6 Mon Sep 17 00:00:00 2001 From: Ray Wang Date: Sun, 21 Nov 2021 14:21:56 +0800 Subject: [PATCH] deps: V8: cherry-pick cced52a97ee9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Original commit message: [date] Skip leading zeros when parsing date string 1. Skip leading zeros when parsing date string 2. Add necessary unittests Bug: v8:12256 Change-Id: Ibc1f320382a2e33175f7f57542c8fe48afd05fa8 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3223239 Reviewed-by: Shu-yu Guo Reviewed-by: Igor Sheludko Commit-Queue: Shu-yu Guo Cr-Commit-Position: refs/heads/main@{#77592} Refs: https://github.com/v8/v8/commit/cced52a97ee954fedd6187abfd042c95a1cce6c6 PR-URL: https://github.com/nodejs/node/pull/40656 Reviewed-By: Colin Ihrig Reviewed-By: Michaƫl Zasso Reviewed-By: Richard Lau Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: James M Snell --- common.gypi | 2 +- deps/v8/src/date/dateparser.h | 3 ++ deps/v8/test/mjsunit/regress/regress-12256.js | 54 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 deps/v8/test/mjsunit/regress/regress-12256.js diff --git a/common.gypi b/common.gypi index c77038c3cee21b..4113515bd1a592 100644 --- a/common.gypi +++ b/common.gypi @@ -36,7 +36,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.14', + 'v8_embedder_string': '-node.15', ##### V8 defaults for Node.js ##### diff --git a/deps/v8/src/date/dateparser.h b/deps/v8/src/date/dateparser.h index 9975737c073af3..1a0a0b15ab7585 100644 --- a/deps/v8/src/date/dateparser.h +++ b/deps/v8/src/date/dateparser.h @@ -75,6 +75,9 @@ class DateParser : public AllStatic { int ReadUnsignedNumeral() { int n = 0; int i = 0; + // First, skip leading zeros + while (ch_ == '0') Next(); + // And then, do the conversion while (IsAsciiDigit()) { if (i < kMaxSignificantDigits) n = n * 10 + ch_ - '0'; i++; diff --git a/deps/v8/test/mjsunit/regress/regress-12256.js b/deps/v8/test/mjsunit/regress/regress-12256.js new file mode 100644 index 00000000000000..e6407c06ed1df2 --- /dev/null +++ b/deps/v8/test/mjsunit/regress/regress-12256.js @@ -0,0 +1,54 @@ +// Copyright 2021 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +const dates = [{ year: '2021', month: '10', day: '22', hour: '10', minute: '12', second: '32' }, + { year: '2021', month: '8', day: '3', hour: '9', minute: '9', second: '6' }]; + +for (let date of dates) { + const { year, month, day, hour, minute, second } = date; + const s0 = `${year}-${month}-${day} ${hour}:${minute}:${second}Z`; + + // V8 reads at most kMaxSignificantDigits (9) to build the value of a numeral, + // so let's test up to 9 leading zeros. + + // For years + for (let i = 1; i < 10; i++) { + const s1 = `${'0'.repeat(i) + year}-${month}-${day} ${hour}:${minute}:${second}Z`; + assertTrue(new Date(s0).getTime() == new Date(s1).getTime()); + } + + // For months + for (let i = 1; i < 10; i++) { + const s1 = `${year}-${'0'.repeat(i) + month}-${day} ${hour}:${minute}:${second}Z`; + assertTrue(new Date(s0).getTime() == new Date(s1).getTime()); + } + + // For days + for (let i = 1; i < 10; i++) { + const s1 = `${year}-${month}-${'0'.repeat(i) + day} ${hour}:${minute}:${second}Z`; + assertTrue(new Date(s0).getTime() == new Date(s1).getTime()); + } + + // For hours + for (let i = 1; i < 10; i++) { + const s1 = `${year}-${month}-${day} ${'0'.repeat(i) + hour}:${minute}:${second}Z`; + assertTrue(new Date(s0).getTime() == new Date(s1).getTime()); + } + + // For minutes + for (let i = 1; i < 10; i++) { + const s1 = `${year}-${month}-${day} ${hour}:${'0'.repeat(i) + minute}:${second}Z`; + assertTrue(new Date(s0).getTime() == new Date(s1).getTime()); + } + + // For seconds + for (let i = 1; i < 10; i++) { + const s1 = `${year}-${month}-${day} ${hour}:${minute}:${'0'.repeat(i) + second}Z`; + assertTrue(new Date(s0).getTime() == new Date(s1).getTime()); + } + + // With same input date string, + // Date() and Date.parse() should return the same date + assertTrue(new Date(s0).getTime() == Date.parse(s0)); +}