Commit 3364acfe authored by Jungshik Shin's avatar Jungshik Shin Committed by Commit Bot

Speculative fix and test for crash

Add a check to SetICUDefault() and fall back to en-US when an incoming
locale id fails to create a valid icu::locale instance.

Roll ICU to b971435d43 to pick up an upstream fix.

icu::Locale::setDefault() can fail when an invalid/too long locale id is
passed. It appears that this might happen when the locale id comes from
GetOverrideLocale() on iOS.

A test added is similar to what the upstream added in the upstream fix (
http://bugs.icu-project.org/trac/changeset/40267 ) which was cherry-picked in
https://chromium-review.googlesource.com/c/574935/ .


BUG=chromium:734093
TEST=base_unittests --gtest_filter=SetICU*

Change-Id: If8d3522b23a257466ca3c31b66089cb415fec3a2
Reviewed-on: https://chromium-review.googlesource.com/574936Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Commit-Queue: Jungshik Shin <jshin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487533}
parent 8d3ceadb
......@@ -157,7 +157,7 @@ deps = {
Var('chromium_git') + '/external/github.com/google/googletest.git' + '@' + '42bc671f47b122fad36db5eccbc06868afdf7862',
'src/third_party/icu':
Var('chromium_git') + '/chromium/deps/icu.git' + '@' + 'dfa798fe694702b43a3debc3290761f22b1acaf8',
Var('chromium_git') + '/chromium/deps/icu.git' + '@' + 'b971435d43d012e9c49ef5e99c0c3047640d53be',
'src/third_party/hunspell_dictionaries':
Var('chromium_git') + '/chromium/deps/hunspell_dictionaries.git' + '@' + 'dc6e7c25bf47cbfb466e0701fd2728b4a12e79d5',
......
......@@ -156,12 +156,14 @@ std::string ICULocaleName(const std::string& locale_string) {
void SetICUDefaultLocale(const std::string& locale_string) {
icu::Locale locale(ICULocaleName(locale_string).c_str());
UErrorCode error_code = U_ZERO_ERROR;
icu::Locale::setDefault(locale, error_code);
// This return value is actually bogus because Locale object is
// an ID and setDefault seems to always succeed (regardless of the
// presence of actual locale data). However,
// it does not hurt to have it as a sanity check.
DCHECK(U_SUCCESS(error_code));
const char* lang = locale.getLanguage();
if (lang != nullptr && *lang != '\0') {
icu::Locale::setDefault(locale, error_code);
} else {
LOG(ERROR) << "Failed to set the ICU default locale to " << locale_string
<< ". Falling back to en-US.";
icu::Locale::setDefault(icu::Locale::getUS(), error_code);
}
g_icu_text_direction = UNKNOWN_DIRECTION;
}
......
......@@ -17,6 +17,7 @@
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#include "third_party/icu/source/common/unicode/locid.h"
#include "third_party/icu/source/i18n/unicode/usearch.h"
namespace base {
......@@ -467,5 +468,19 @@ TEST_F(RTLTest, UnadjustStringForLocaleDirection) {
EXPECT_EQ(was_rtl, IsRTL());
}
class SetICULocaleTest : public PlatformTest {};
TEST_F(SetICULocaleTest, OverlongLocaleId) {
test::ScopedRestoreICUDefaultLocale restore_locale;
std::string id("fr-ca-x-foo");
while (id.length() < 152)
id.append("-x-foo");
SetICUDefaultLocale(id);
EXPECT_STRNE("en_US", icu::Locale::getDefault().getName());
id.append("zzz");
SetICUDefaultLocale(id);
EXPECT_STREQ("en_US", icu::Locale::getDefault().getName());
}
} // namespace i18n
} // namespace base
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment