From 6dfa3a52d0768002ad0055df1a4d1bfb0c1575e7 Mon Sep 17 00:00:00 2001 From: Yoshito Umaoka Date: Mon, 7 Nov 2011 16:51:49 +0000 Subject: [PATCH] ICU-8927 Avoid NPE when invalid system ID is specifed in TimeZone.countEquivalentIDs and getEquivalentID. X-SVN-Rev: 30937 --- .../core/src/com/ibm/icu/impl/ZoneMeta.java | 50 ++++++++++--------- .../icu/dev/test/timezone/TimeZoneTest.java | 15 ++++++ 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/ZoneMeta.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/ZoneMeta.java index f79eecfab69..56ffa5dd357 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/ZoneMeta.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/ZoneMeta.java @@ -218,13 +218,15 @@ public final class ZoneMeta { */ public static synchronized int countEquivalentIDs(String id) { int count = 0; - try { - UResourceBundle res = openOlsonResource(null, id); - UResourceBundle links = res.get("links"); - int[] v = links.getIntVector(); - count = v.length; - } catch (MissingResourceException ex) { - // throw away + UResourceBundle res = openOlsonResource(null, id); + if (res != null) { + try { + UResourceBundle links = res.get("links"); + int[] v = links.getIntVector(); + count = v.length; + } catch (MissingResourceException ex) { + // throw away + } } return count; } @@ -249,25 +251,25 @@ public final class ZoneMeta { */ public static synchronized String getEquivalentID(String id, int index) { String result = ""; - int zoneIdx = -1; - if (index >= 0) { - try { - UResourceBundle res = openOlsonResource(null, id); - UResourceBundle links = res.get("links"); - int[] zones = links.getIntVector(); - if (index < zones.length) { - zoneIdx = zones[index]; + UResourceBundle res = openOlsonResource(null, id); + if (res != null) { + int zoneIdx = -1; + try { + UResourceBundle links = res.get("links"); + int[] zones = links.getIntVector(); + if (index < zones.length) { + zoneIdx = zones[index]; + } + } catch (MissingResourceException ex) { + // throw away + } + if (zoneIdx >= 0) { + String tmp = getZoneID(zoneIdx); + if (tmp != null) { + result = tmp; + } } - } catch (MissingResourceException ex) { - // throw away - zoneIdx = -1; - } - } - if (zoneIdx >= 0) { - String tmp = getZoneID(zoneIdx); - if (tmp != null) { - result = tmp; } } return result; diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/timezone/TimeZoneTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/timezone/TimeZoneTest.java index ba2783d113e..b5ef8f4efb5 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/timezone/TimeZoneTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/timezone/TimeZoneTest.java @@ -1019,6 +1019,21 @@ public class TimeZoneTest extends TestFmwk ") returned \"" + outOfRangeID + "\", expected empty string"); } } + + // Ticket#8927 invalid system ID + final String[] invaldIDs = {"GMT-05:00", "Hello World!", ""}; + for (String invld : invaldIDs) { + int nEquiv = TimeZone.countEquivalentIDs(invld); + if (nEquiv != 0) { + errln("FAIL: countEquivalentIDs(" + invld + ") returned: " + nEquiv + + ", expected: 0"); + } + String sEquiv0 = TimeZone.getEquivalentID(invld, 0); + if (sEquiv0.length() > 0) { + errln("FAIL: getEquivalentID(" + invld + ", 0) returned \"" + sEquiv0 + + "\", expected empty string"); + } + } } public void TestCountries() { -- 2.40.0