From e3358e77f524f4d448c2ebb7c5afd0aa432f0d44 Mon Sep 17 00:00:00 2001 From: Naoto Sato Date: Tue, 20 Sep 2022 16:46:18 +0000 Subject: [PATCH] 8294008: Grapheme implementation of setText() throws IndexOutOfBoundsException Reviewed-by: joehw, smarks --- .../provider/BreakIteratorProviderImpl.java | 15 +++++++++++++-- .../text/BreakIterator/BreakIteratorTest.java | 5 +++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/java.base/share/classes/sun/util/locale/provider/BreakIteratorProviderImpl.java b/src/java.base/share/classes/sun/util/locale/provider/BreakIteratorProviderImpl.java index 4ed8a5b2363..3f531c9a5f9 100644 --- a/src/java.base/share/classes/sun/util/locale/provider/BreakIteratorProviderImpl.java +++ b/src/java.base/share/classes/sun/util/locale/provider/BreakIteratorProviderImpl.java @@ -317,7 +317,15 @@ public class BreakIteratorProviderImpl extends BreakIteratorProvider } } - // Implementation only for calling Grapheme.nextBoundary() + /** + * Implementation only for calling Grapheme.nextBoundary(). + * + * This is a special-purpose CharSequence that represents characters in the + * index range [0..endIndex) of the underlying CharacterIterator, even if + * that CharacterIterator represents the subrange of some string. The calling + * code in GraphemeBreakIterator takes care to ensure that only valid indexes + * into the src are used. + */ static final class CharacterIteratorCharSequence implements CharSequence { CharacterIterator src; CharacterIteratorCharSequence(CharacterIterator ci) { @@ -326,7 +334,10 @@ public class BreakIteratorProviderImpl extends BreakIteratorProvider @Override public int length() { - return src.getEndIndex() - src.getBeginIndex(); + // Return the entire CharSequence length (0 to endIndex), not to + // be confused with the text range length (beginIndex to endIndex) + // of the underlying CharacterIterator. + return src.getEndIndex(); } @Override diff --git a/test/jdk/java/text/BreakIterator/BreakIteratorTest.java b/test/jdk/java/text/BreakIterator/BreakIteratorTest.java index 9ba2d290ab5..5a29ca86751 100644 --- a/test/jdk/java/text/BreakIterator/BreakIteratorTest.java +++ b/test/jdk/java/text/BreakIterator/BreakIteratorTest.java @@ -26,6 +26,7 @@ * @bug 4035266 4052418 4068133 4068137 4068139 4086052 4095322 4097779 * 4097920 4098467 4111338 4113835 4117554 4143071 4146175 4152117 * 4152416 4153072 4158381 4214367 4217703 4638433 8264765 8291660 + * 8294008 * @library /java/text/testlib * @run main/timeout=2000 BreakIteratorTest * @summary test BreakIterator @@ -1468,4 +1469,8 @@ public class BreakIteratorTest extends IntlTest generalIteratorTest(characterBreak, expected); }); } + + public void TestSetTextIOOBException() { + BreakIterator.getCharacterInstance().setText(new StringCharacterIterator("abcfefg", 1, 5, 3)); + } }