提交 c51d2cc3 编写于 作者: N naoto

7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances

Reviewed-by: okutsu
上级 2f66a543
...@@ -45,6 +45,7 @@ import java.lang.ref.SoftReference; ...@@ -45,6 +45,7 @@ import java.lang.ref.SoftReference;
import java.text.spi.DateFormatSymbolsProvider; import java.text.spi.DateFormatSymbolsProvider;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
...@@ -366,6 +367,7 @@ public class DateFormatSymbols implements Serializable, Cloneable { ...@@ -366,6 +367,7 @@ public class DateFormatSymbols implements Serializable, Cloneable {
*/ */
public void setEras(String[] newEras) { public void setEras(String[] newEras) {
eras = Arrays.copyOf(newEras, newEras.length); eras = Arrays.copyOf(newEras, newEras.length);
cachedHashCode = 0;
} }
/** /**
...@@ -393,6 +395,7 @@ public class DateFormatSymbols implements Serializable, Cloneable { ...@@ -393,6 +395,7 @@ public class DateFormatSymbols implements Serializable, Cloneable {
*/ */
public void setMonths(String[] newMonths) { public void setMonths(String[] newMonths) {
months = Arrays.copyOf(newMonths, newMonths.length); months = Arrays.copyOf(newMonths, newMonths.length);
cachedHashCode = 0;
} }
/** /**
...@@ -420,6 +423,7 @@ public class DateFormatSymbols implements Serializable, Cloneable { ...@@ -420,6 +423,7 @@ public class DateFormatSymbols implements Serializable, Cloneable {
*/ */
public void setShortMonths(String[] newShortMonths) { public void setShortMonths(String[] newShortMonths) {
shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length); shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length);
cachedHashCode = 0;
} }
/** /**
...@@ -439,6 +443,7 @@ public class DateFormatSymbols implements Serializable, Cloneable { ...@@ -439,6 +443,7 @@ public class DateFormatSymbols implements Serializable, Cloneable {
*/ */
public void setWeekdays(String[] newWeekdays) { public void setWeekdays(String[] newWeekdays) {
weekdays = Arrays.copyOf(newWeekdays, newWeekdays.length); weekdays = Arrays.copyOf(newWeekdays, newWeekdays.length);
cachedHashCode = 0;
} }
/** /**
...@@ -458,6 +463,7 @@ public class DateFormatSymbols implements Serializable, Cloneable { ...@@ -458,6 +463,7 @@ public class DateFormatSymbols implements Serializable, Cloneable {
*/ */
public void setShortWeekdays(String[] newShortWeekdays) { public void setShortWeekdays(String[] newShortWeekdays) {
shortWeekdays = Arrays.copyOf(newShortWeekdays, newShortWeekdays.length); shortWeekdays = Arrays.copyOf(newShortWeekdays, newShortWeekdays.length);
cachedHashCode = 0;
} }
/** /**
...@@ -474,6 +480,7 @@ public class DateFormatSymbols implements Serializable, Cloneable { ...@@ -474,6 +480,7 @@ public class DateFormatSymbols implements Serializable, Cloneable {
*/ */
public void setAmPmStrings(String[] newAmpms) { public void setAmPmStrings(String[] newAmpms) {
ampms = Arrays.copyOf(newAmpms, newAmpms.length); ampms = Arrays.copyOf(newAmpms, newAmpms.length);
cachedHashCode = 0;
} }
/** /**
...@@ -558,6 +565,7 @@ public class DateFormatSymbols implements Serializable, Cloneable { ...@@ -558,6 +565,7 @@ public class DateFormatSymbols implements Serializable, Cloneable {
} }
zoneStrings = aCopy; zoneStrings = aCopy;
isZoneStringsSet = true; isZoneStringsSet = true;
cachedHashCode = 0;
} }
/** /**
...@@ -576,6 +584,7 @@ public class DateFormatSymbols implements Serializable, Cloneable { ...@@ -576,6 +584,7 @@ public class DateFormatSymbols implements Serializable, Cloneable {
public void setLocalPatternChars(String newLocalPatternChars) { public void setLocalPatternChars(String newLocalPatternChars) {
// Call toString() to throw an NPE in case the argument is null // Call toString() to throw an NPE in case the argument is null
localPatternChars = newLocalPatternChars.toString(); localPatternChars = newLocalPatternChars.toString();
cachedHashCode = 0;
} }
/** /**
...@@ -597,12 +606,23 @@ public class DateFormatSymbols implements Serializable, Cloneable { ...@@ -597,12 +606,23 @@ public class DateFormatSymbols implements Serializable, Cloneable {
* Override hashCode. * Override hashCode.
* Generates a hash code for the DateFormatSymbols object. * Generates a hash code for the DateFormatSymbols object.
*/ */
@Override
public int hashCode() { public int hashCode() {
int hashcode = 0; int hashCode = cachedHashCode;
String[][] zoneStrings = getZoneStringsWrapper(); if (hashCode == 0) {
for (int index = 0; index < zoneStrings[0].length; ++index) hashCode = 5;
hashcode ^= zoneStrings[0][index].hashCode(); hashCode = 11 * hashCode + Arrays.hashCode(eras);
return hashcode; hashCode = 11 * hashCode + Arrays.hashCode(months);
hashCode = 11 * hashCode + Arrays.hashCode(shortMonths);
hashCode = 11 * hashCode + Arrays.hashCode(weekdays);
hashCode = 11 * hashCode + Arrays.hashCode(shortWeekdays);
hashCode = 11 * hashCode + Arrays.hashCode(ampms);
hashCode = 11 * hashCode + Arrays.deepHashCode(getZoneStringsWrapper());
hashCode = 11 * hashCode + Objects.hashCode(localPatternChars);
cachedHashCode = hashCode;
}
return hashCode;
} }
/** /**
...@@ -641,6 +661,11 @@ public class DateFormatSymbols implements Serializable, Cloneable { ...@@ -641,6 +661,11 @@ public class DateFormatSymbols implements Serializable, Cloneable {
private transient int lastZoneIndex = 0; private transient int lastZoneIndex = 0;
/**
* Cached hash code
*/
transient volatile int cachedHashCode = 0;
private void initializeData(Locale desiredLocale) { private void initializeData(Locale desiredLocale) {
locale = desiredLocale; locale = desiredLocale;
...@@ -782,6 +807,7 @@ public class DateFormatSymbols implements Serializable, Cloneable { ...@@ -782,6 +807,7 @@ public class DateFormatSymbols implements Serializable, Cloneable {
dst.zoneStrings = null; dst.zoneStrings = null;
} }
dst.localPatternChars = src.localPatternChars; dst.localPatternChars = src.localPatternChars;
dst.cachedHashCode = 0;
} }
/** /**
......
...@@ -44,6 +44,7 @@ public class DateFormatSymbolsProviderTest extends ProviderTest { ...@@ -44,6 +44,7 @@ public class DateFormatSymbolsProviderTest extends ProviderTest {
DateFormatSymbolsProviderTest() { DateFormatSymbolsProviderTest() {
availableLocalesTest(); availableLocalesTest();
objectValidityTest(); objectValidityTest();
hashCodeTest();
} }
void availableLocalesTest() { void availableLocalesTest() {
...@@ -124,4 +125,17 @@ public class DateFormatSymbolsProviderTest extends ProviderTest { ...@@ -124,4 +125,17 @@ public class DateFormatSymbolsProviderTest extends ProviderTest {
} }
} }
} }
// Bug 7200341.
void hashCodeTest() {
for (Locale target: availloc) {
// look for provider's object
DateFormatSymbols dfs = DateFormatSymbols.getInstance(target);
if (dfs.getClass().getSimpleName().equals("FooDateFormatSymbols")) {
// call its hashCode(). success if no ArrayIndexOutOfBoundsException is thrown.
dfs.hashCode();
break;
}
}
}
} }
...@@ -23,6 +23,6 @@ ...@@ -23,6 +23,6 @@
#!/bin/sh #!/bin/sh
# #
# @test # @test
# @bug 4052440 # @bug 4052440 7200341
# @summary DateFormatSymbolsProvider tests # @summary DateFormatSymbolsProvider tests
# @run shell ExecTest.sh foo DateFormatSymbolsProviderTest true # @run shell ExecTest.sh foo DateFormatSymbolsProviderTest true
...@@ -221,5 +221,10 @@ public class DateFormatSymbolsProviderImpl extends DateFormatSymbolsProvider { ...@@ -221,5 +221,10 @@ public class DateFormatSymbolsProviderImpl extends DateFormatSymbolsProvider {
public void setAmPmStrings(String[] newAmpms) { public void setAmPmStrings(String[] newAmpms) {
ampms = newAmpms; ampms = newAmpms;
} }
@Override
public String[][] getZoneStrings() {
return new String[0][0];
}
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册