--- old/src/java.base/share/classes/java/text/MessageFormat.java 2017-12-01 12:15:23.467531910 +0530 +++ new/src/java.base/share/classes/java/text/MessageFormat.java 2017-12-01 12:15:23.223409910 +0530 @@ -701,6 +701,10 @@ * larger than the number of format elements in the pattern string */ public void setFormat(int formatElementIndex, Format newFormat) { + + if (formatElementIndex > maxOffset) { + throw new ArrayIndexOutOfBoundsException(formatElementIndex); + } formats[formatElementIndex] = newFormat; } --- /dev/null 2017-12-01 11:27:04.204000000 +0530 +++ new/test/jdk/java/text/Format/MessageFormat/Bug8187551.java 2017-12-01 12:15:23.755675909 +0530 @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +/* + * @test + * @bug 8187551 + * @summary test MessageFormat.setFormat() method to throw AIOOBE on invalid + * index + */ + +import java.text.ChoiceFormat; +import java.text.MessageFormat; +import java.util.Arrays; + +public class Bug8187551 { + + public static void main(String[] args) { + + //invalid cases ("pattern", "invalid format element index") + String[][] invalidCases = {{"The disk \"{1}\" contains {0}.", "2"}, + {"The disk \"{1}\" contains {0}.", "9"}, + {"On {1}, there are {0} and {2} folders", "3"}}; + + //invalid cases (must throw exception) + Arrays.stream(invalidCases).forEach(entry -> testSetFormat(entry[0], + Integer.valueOf(entry[1]))); + + } + + // test the setFormat method for the given pattern and + // format element index + private static void testSetFormat(String pattern, int elemIndex) { + MessageFormat form = new MessageFormat(pattern); + + double[] fileLimits = {0, 1, 2}; + String[] filePart = {"no files", "one file", "{0,number} files"}; + ChoiceFormat fileForm = new ChoiceFormat(fileLimits, filePart); + + boolean AIOOBEThrown = false; + try { + form.setFormat(elemIndex, fileForm); + } catch (ArrayIndexOutOfBoundsException ex) { + AIOOBEThrown = true; + } + + if (!AIOOBEThrown) { + throw new RuntimeException("[FAILED: Must throw" + + " ArrayIndexOutOfBoundsException for" + + " invalid index " + elemIndex + " used in" + + " MessageFormat.setFormat(index, format)]"); + } + } + +}