Name: bsC130419 Date: 06/26/2001
java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)
Pattern.compile() throws an ArrayIndexOutOfBoundsException if it's passed a
long quoted string:
public void testShortQuote() {
String test = "\\Qshort (22 char) quote\\E";
Pattern p = Pattern.compile(test);
assert(p.pattern()==test);
}
public void testLongQuote() {
String test = "\\QThis is a long (>32 character) quoted string\\E";
Pattern p = Pattern.compile(test);
assert(p.pattern()==test);
}
testShortQuote() runs fine, but testLongQuote() fails with:
java.lang.ArrayIndexOutOfBoundsException
at java.util.regex.Pattern.append(Pattern.java:1562)
at java.util.regex.Pattern.escape(Pattern.java:1647)
at java.util.regex.Pattern.atom(Pattern.java:1524)
at java.util.regex.Pattern.sequence(Pattern.java:1471)
at java.util.regex.Pattern.expr(Pattern.java:1383)
at java.util.regex.Pattern.compile(Pattern.java:1231)
in Pattern.append():
private void append(int ch, int len) {
char[] buf = buffer;
if (len >= buf.length) {
char[] tmp = new char[len+len];
for (int i = 0; i < len; i++) {
tmp[i] = buf[i];
}
buf = tmp;
}
buf[len] = (char) ch;
}
If buffer is not big enough the temporary variable buf is increased in size -
but the result is never assigned back to buffer and is lost. The
ArrayOutOfBounds exception occurs on the following char at the tmp[i] = buf[len-
1] assignment
(Review ID: 127387)
======================================================================
- duplicates
-
JDK-4465339 Long patterns cause ArrayIndexOutOfBoundsException
-
- Closed
-