]> granicus.if.org Git - icu/commitdiff
ICU-13312 Fixed getDisplayName crash issue caused by TZDBTimeZoneNames, added a test...
authorYoshito Umaoka <y.umaoka@gmail.com>
Thu, 14 Sep 2017 02:37:44 +0000 (02:37 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Thu, 14 Sep 2017 02:37:44 +0000 (02:37 +0000)
X-SVN-Rev: 40412

icu4c/source/i18n/tznames_impl.cpp
icu4c/source/i18n/tznames_impl.h
icu4c/source/test/intltest/tzfmttst.cpp
icu4c/source/test/intltest/tzfmttst.h

index 8accb84d2d5b67fa8a113f711a3313009402d6f0..7127b176751d187b6925b64304d907dd2702713e 100644 (file)
@@ -2056,6 +2056,9 @@ static void U_CALLCONV prepareFind(UErrorCode &status) {
     if (U_SUCCESS(status)) {
         while ((mzID = mzIDs->snext(status)) && U_SUCCESS(status)) {
             const TZDBNames *names = TZDBTimeZoneNames::getMetaZoneNames(*mzID, status);
+            if (U_FAILURE(status)) {
+                break;
+            }
             if (names == NULL) {
                 continue;
             }
@@ -2187,9 +2190,11 @@ TZDBTimeZoneNames::getMetaZoneDisplayName(const UnicodeString& mzID,
     UErrorCode status = U_ZERO_ERROR;
     const TZDBNames *tzdbNames = TZDBTimeZoneNames::getMetaZoneNames(mzID, status);
     if (U_SUCCESS(status)) {
-        const UChar *s = tzdbNames->getName(type);
-        if (s != NULL) {
-            name.setTo(TRUE, s, -1);
+        if (tzdbNames != NULL) {
+            const UChar *s = tzdbNames->getName(type);
+            if (s != NULL) {
+                name.setTo(TRUE, s, -1);
+            }
         }
     }
 
index 9251f9ef470f6ec5f4ee9436402c45bb9bdd70f6..0aac1de5545802155037ae9677342bcde916847f 100644 (file)
@@ -246,6 +246,8 @@ public:
 
     TimeZoneNames::MatchInfoCollection* find(const UnicodeString& text, int32_t start, uint32_t types, UErrorCode& status) const;
 
+    // When TZDBNames for the metazone is not available, this method returns NULL,
+    // but does NOT set U_MISSING_RESOURCE_ERROR to status.
     static const TZDBNames* getMetaZoneNames(const UnicodeString& mzId, UErrorCode& status);
 
 private:
index 9557b144921494b812e711c0ee4953a7e27f39a4..b74ee5ffc719add5e69e5d4ef02759e3ffa2433c 100644 (file)
@@ -83,7 +83,8 @@ TimeZoneFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &name
         TESTCASE(4, TestFormat);
         TESTCASE(5, TestFormatTZDBNames);
         TESTCASE(6, TestFormatCustomZone);
-        default: name = ""; break;
+        TESTCASE(7, TestFormatTZDBNamesAllZoneCoverage);
+    default: name = ""; break;
     }
 }
 
@@ -1251,5 +1252,44 @@ TimeZoneFormatTest::TestFormatCustomZone(void) {
     }
 }
 
+void
+TimeZoneFormatTest::TestFormatTZDBNamesAllZoneCoverage(void) {
+    UErrorCode status = U_ZERO_ERROR;
+    LocalPointer<StringEnumeration> tzids(TimeZone::createEnumeration());
+    const UnicodeString *tzid;
+    LocalPointer<TimeZoneNames> tzdbNames(TimeZoneNames::createTZDBInstance(Locale("en"), status));
+    UDate now = Calendar::getNow();
+    UnicodeString mzId;
+    UnicodeString name;
+    while ((tzid = tzids->snext(status))) {
+        logln("Zone: " + *tzid);
+        LocalPointer<TimeZone> tz(TimeZone::createTimeZone(*tzid));
+        tzdbNames->getMetaZoneID(*tzid, now, mzId);
+        if (mzId.isBogus()) {
+            logln((UnicodeString)"Meta zone: <not available>");
+        } else {
+            logln((UnicodeString)"Meta zone: " + mzId);
+        }
+
+        // mzID could be bogus here
+        tzdbNames->getMetaZoneDisplayName(mzId, UTZNM_SHORT_STANDARD, name);
+        // name could be bogus here
+        if (name.isBogus()) {
+            logln((UnicodeString)"Meta zone short standard name: <not available>");
+        }
+        else {
+            logln((UnicodeString)"Meta zone short standard name: " + name);
+        }
+
+        tzdbNames->getMetaZoneDisplayName(mzId, UTZNM_SHORT_DAYLIGHT, name);
+        // name could be bogus here
+        if (name.isBogus()) {
+            logln((UnicodeString)"Meta zone short daylight name: <not available>");
+        }
+        else {
+            logln((UnicodeString)"Meta zone short daylight name: " + name);
+        }
+    }
+}
 
 #endif /* #if !UCONFIG_NO_FORMATTING */
index 0bf91ae0e8ea5d10affe26172ed40822a71a07f6..89ece5e2e06e6ddbc322676115fbf1ed98c58eeb 100644 (file)
@@ -28,6 +28,7 @@ class TimeZoneFormatTest : public IntlTest {
     void TestFormat(void);
     void TestFormatTZDBNames(void);
     void TestFormatCustomZone(void);
+    void TestFormatTZDBNamesAllZoneCoverage(void);
 
     void RunTimeRoundTripTests(int32_t threadNumber);
 };