[Intl] Fix RelativeTimeFormat fatal

Intl.RelativeTimeFormat constructor crash while the locale or
numberingSystem contains an "algorithmic" numberingSystem.
Fix by fallback to the locale without the nu

Bug: chromium:1041319
Change-Id: Ica520e8dec6ace21264504274b92cb2c3d16286f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2055970
Reviewed-by: Shu-yu Guo <syg@chromium.org>
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66276}
This commit is contained in:
Frank Tang 2020-02-13 15:29:23 -08:00 committed by Commit Bot
parent ffaa1fe555
commit a872c393c6
2 changed files with 66 additions and 5 deletions

View File

@ -132,10 +132,6 @@ MaybeHandle<JSRelativeTimeFormat> JSRelativeTimeFormat::New(
icu_locale.setUnicodeKeywordValue("nu", numbering_system_str.get(), status);
CHECK(U_SUCCESS(status));
}
Handle<String> numbering_system_string =
isolate->factory()->NewStringFromAsciiChecked(
Intl::GetNumberingSystem(icu_locale).c_str());
// 15. Let dataLocale be r.[[DataLocale]].
// 16. Let s be ? GetOption(options, "style", "string",
@ -165,7 +161,21 @@ MaybeHandle<JSRelativeTimeFormat> JSRelativeTimeFormat::New(
icu::NumberFormat::createInstance(icu_locale, UNUM_DECIMAL, status);
if (U_FAILURE(status)) {
delete number_format;
FATAL("Failed to create ICU number format, are ICU data files missing?");
// Data build filter files excluded data in "rbnf_tree" since ECMA402 does
// not support "algorithmic" numbering systems. Therefore we may get the
// U_MISSING_RESOURCE_ERROR here. Fallback to locale without the numbering
// system and create the object again.
if (status == U_MISSING_RESOURCE_ERROR) {
status = U_ZERO_ERROR;
icu_locale.setUnicodeKeywordValue("nu", nullptr, status);
CHECK(U_SUCCESS(status));
number_format =
icu::NumberFormat::createInstance(icu_locale, UNUM_DECIMAL, status);
}
if (U_FAILURE(status)) {
delete number_format;
FATAL("Failed to create ICU number format, are ICU data files missing?");
}
}
CHECK_NOT_NULL(number_format);
@ -191,6 +201,11 @@ MaybeHandle<JSRelativeTimeFormat> JSRelativeTimeFormat::New(
Handle<JSRelativeTimeFormat> relative_time_format_holder =
Handle<JSRelativeTimeFormat>::cast(
isolate->factory()->NewFastOrSlowJSObjectFromMap(map));
Handle<String> numbering_system_string =
isolate->factory()->NewStringFromAsciiChecked(
Intl::GetNumberingSystem(icu_locale).c_str());
DisallowHeapAllocation no_gc;
relative_time_format_holder->set_flags(0);
relative_time_format_holder->set_locale(*locale_str);

View File

@ -0,0 +1,46 @@
// Copyright 2020 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.
// Test to check "algorithmic" numbering systems stated in UTS35 but not
// mandated by ECMA402 won't crash.
// The entries which type is "algorithmic" in
// https://github.com/unicode-org/cldr/blob/master/common/supplemental/numberingSystems.xml
// These are numbering systems which is not supported in ECMA402 but we should
// not crash.
let algorithmicNumberingSystems = [
"armn",
"armnlow",
"cyrl",
"ethi",
"geor",
"grek",
"greklow",
"hanidays",
"hans",
"hansfin",
"hant",
"hantfin",
"hebr",
"jpan",
"jpanfin",
"jpanyear",
"roman",
"romanlow",
"taml",
];
for (numberingSystem of algorithmicNumberingSystems) {
let baseLocale = "en";
let locale = baseLocale + "-u-nu-" + numberingSystem;
// Ensure the creation won't crash
let rtf = new Intl.RelativeTimeFormat(locale);
let rtf2 = new Intl.RelativeTimeFormat(baseLocale, {numberingSystem});
let dtf = new Intl.DateTimeFormat(locale);
let dtf2 = new Intl.DateTimeFormat(baseLocale, {numberingSystem});
let nf = new Intl.NumberFormat(locale);
let nf2 = new Intl.NumberFormat(baseLocale, {numberingSystem});
}