diff options
author | Victor Chang <vichang@google.com> | 2021-03-19 16:50:37 +0000 |
---|---|---|
committer | Victor Chang <vichang@google.com> | 2021-03-22 17:43:32 +0000 |
commit | 96ae031e8caa2c193cb9c34aa93d2617024db207 (patch) | |
tree | 89667d3320a02c83a5809e1e47c8a8d361cea27d /android_icu4j | |
parent | fb32eedde7c3c7e83d709fed9f6e499bf5808fb5 (diff) | |
download | icu-96ae031e8caa2c193cb9c34aa93d2617024db207.tar.gz |
ScopedIcuLocale incorrectly parses the input as locale ID
All usages of ScopedIcuLocale are in LocaleNative, CaseMapperNative,
TimeZoneNamesNative, and they provided a BCP-47 language tag
instead of ICU locale ID. ScopedIcuLocale should parse the input
as language tag.
The tests for those 3 classes are added in the prior CLs.
The bug wasn't an issue because Locale::createFromName is lenient enought
to parse language tag, espesically accepting both - and _ as the
separator. However, it's not guaranteed in the future ICU version.
No known regression is caused by this bug.
Test: CtsLibcoreTestCases
Test: CtsIcuTestCases
Change-Id: Iea107619d638e06d58623b702793959df1210e68
Diffstat (limited to 'android_icu4j')
-rw-r--r-- | android_icu4j/libcore_bridge/src/native/ScopedIcuLocale.h | 18 | ||||
-rw-r--r-- | android_icu4j/libcore_bridge/src/native/com_android_icu_text_TimeZoneNamesNative.cpp | 4 |
2 files changed, 14 insertions, 8 deletions
diff --git a/android_icu4j/libcore_bridge/src/native/ScopedIcuLocale.h b/android_icu4j/libcore_bridge/src/native/ScopedIcuLocale.h index 851de76c6..6d707d02f 100644 --- a/android_icu4j/libcore_bridge/src/native/ScopedIcuLocale.h +++ b/android_icu4j/libcore_bridge/src/native/ScopedIcuLocale.h @@ -23,20 +23,26 @@ class ScopedIcuLocale { public: - ScopedIcuLocale(JNIEnv* env, jstring javaLocaleName) : mEnv(env) { + ScopedIcuLocale(JNIEnv* env, jstring javaLanguageTag) : mEnv(env) { mLocale.setToBogus(); - if (javaLocaleName == NULL) { - jniThrowNullPointerException(mEnv, "javaLocaleName == null"); + if (javaLanguageTag == NULL) { + jniThrowNullPointerException(mEnv, "javaLanguageTag == null"); return; } - const ScopedUtfChars localeName(env, javaLocaleName); - if (localeName.c_str() == NULL) { + const ScopedUtfChars languageTag(env, javaLanguageTag); + if (languageTag.c_str() == NULL) { return; } - mLocale = icu::Locale::createFromName(localeName.c_str()); + UErrorCode err; + icu::Locale locale = icu::Locale::forLanguageTag(languageTag.c_str(), err); + if (U_FAILURE(err)) { + return; + } + + mLocale = locale; } ~ScopedIcuLocale() { diff --git a/android_icu4j/libcore_bridge/src/native/com_android_icu_text_TimeZoneNamesNative.cpp b/android_icu4j/libcore_bridge/src/native/com_android_icu_text_TimeZoneNamesNative.cpp index f3e51f4f2..5b3b05c71 100644 --- a/android_icu4j/libcore_bridge/src/native/com_android_icu_text_TimeZoneNamesNative.cpp +++ b/android_icu4j/libcore_bridge/src/native/com_android_icu_text_TimeZoneNamesNative.cpp @@ -43,8 +43,8 @@ static bool setStringArrayElement(JNIEnv* env, jobjectArray array, int i, const return true; } -static void TimeZoneNamesNative_fillZoneStringsNative(JNIEnv* env, jclass, jstring javaLocaleName, jobjectArray result) { - ScopedIcuLocale icuLocale(env, javaLocaleName); +static void TimeZoneNamesNative_fillZoneStringsNative(JNIEnv* env, jclass, jstring javaLanguageTag, jobjectArray result) { + ScopedIcuLocale icuLocale(env, javaLanguageTag); if (!icuLocale.valid()) { return; } |