From 1f4653910f4e6932dd3c057f78722fc0a357e3b3 Mon Sep 17 00:00:00 2001 From: Xueming Shen Date: Thu, 12 Apr 2012 15:01:41 -0700 Subject: [PATCH] 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio Updated to throw IAE instead. Reviewed-by: lancea --- jdk/src/share/classes/java/util/regex/Matcher.java | 11 +++++++---- jdk/test/java/util/regex/RegExTest.java | 12 ++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/jdk/src/share/classes/java/util/regex/Matcher.java b/jdk/src/share/classes/java/util/regex/Matcher.java index d654790b24c..5e437e6e8be 100644 --- a/jdk/src/share/classes/java/util/regex/Matcher.java +++ b/jdk/src/share/classes/java/util/regex/Matcher.java @@ -759,16 +759,19 @@ public final class Matcher implements MatchResult { char nextChar = replacement.charAt(cursor); if (nextChar == '\\') { cursor++; + if (cursor == replacement.length()) + throw new IllegalArgumentException( + "character to be escaped is missing"); nextChar = replacement.charAt(cursor); result.append(nextChar); cursor++; } else if (nextChar == '$') { // Skip past $ cursor++; - // A StringIndexOutOfBoundsException is thrown if - // this "$" is the last character in replacement - // string in current implementation, a IAE might be - // more appropriate. + // Throw IAE if this "$" is the last character in replacement + if (cursor == replacement.length()) + throw new IllegalArgumentException( + "Illegal group reference: group index is missing"); nextChar = replacement.charAt(cursor); int refNum = -1; if (nextChar == '{') { diff --git a/jdk/test/java/util/regex/RegExTest.java b/jdk/test/java/util/regex/RegExTest.java index 159e9afddd9..f583769ecb2 100644 --- a/jdk/test/java/util/regex/RegExTest.java +++ b/jdk/test/java/util/regex/RegExTest.java @@ -33,6 +33,7 @@ * 5013885 5003322 4988891 5098443 5110268 6173522 4829857 5027748 6376940 * 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133 * 6350801 6676425 6878475 6919132 6931676 6948903 6990617 7014645 7039066 + * 7067045 */ import java.util.regex.*; @@ -852,6 +853,17 @@ public class RegExTest { if (!result.equals(toSupplementaries("zzz\\t$\\$zzz"))) failCount++; + // IAE should be thrown if backslash or '$' is the last character + // in replacement string + try { + "\uac00".replaceAll("\uac00", "$"); + "\uac00".replaceAll("\uac00", "\\"); + failCount++; + } catch (IllegalArgumentException iie) { + } catch (Exception e) { + failCount++; + } + report("Literal replacement"); }