A colleague reports:
MessageFormat.setFormat(int, Format) is documented to throw an ArrayIndexOutOfBoundsException when the index is equal to or larger than the number of format elements in the pattern string.
However, it will only throw that exception when the index is equal to or larger to the size of the internal formats[] array (which is oversized and defaults to 10 elements, unless more than 10 elements exist in the format string).
This means that with a trivial format string with a single element ("{0}") a call to setFormat(1, aFormat) will not throw the exception, but setFormat(10, aFormat) will.
Here's a patch to add a check to throw the appropriate exception instead.
diff -r 65464a307408 src/java.base/share/classes/java/text/MessageFormat.java
--- a/src/java.base/share/classes/java/text/MessageFormat.java Thu Aug 03 18:56:59 2017 +0000
+++ b/src/java.base/share/classes/java/text/MessageFormat.java Thu Sep 14 18:29:08 2017 +0100
@@ -701,6 +701,9 @@
* larger than the number of format elements in the pattern string
*/
public void setFormat(int formatElementIndex, Format newFormat) {
+ if (formatElementIndex > maxOffset) {
+ throw new ArrayIndexOutOfBoundsException(maxOffset, formatElementIndex);
+ }
formats[formatElementIndex] = newFormat;
}
MessageFormat.setFormat(int, Format) is documented to throw an ArrayIndexOutOfBoundsException when the index is equal to or larger than the number of format elements in the pattern string.
However, it will only throw that exception when the index is equal to or larger to the size of the internal formats[] array (which is oversized and defaults to 10 elements, unless more than 10 elements exist in the format string).
This means that with a trivial format string with a single element ("{0}") a call to setFormat(1, aFormat) will not throw the exception, but setFormat(10, aFormat) will.
Here's a patch to add a check to throw the appropriate exception instead.
diff -r 65464a307408 src/java.base/share/classes/java/text/MessageFormat.java
--- a/src/java.base/share/classes/java/text/MessageFormat.java Thu Aug 03 18:56:59 2017 +0000
+++ b/src/java.base/share/classes/java/text/MessageFormat.java Thu Sep 14 18:29:08 2017 +0100
@@ -701,6 +701,9 @@
* larger than the number of format elements in the pattern string
*/
public void setFormat(int formatElementIndex, Format newFormat) {
+ if (formatElementIndex > maxOffset) {
+ throw new ArrayIndexOutOfBoundsException(maxOffset, formatElementIndex);
+ }
formats[formatElementIndex] = newFormat;
}