Skip to content

Commit e58378a

Browse files
committed
8284548: Invalid XPath expression causes StringIndexOutOfBoundsException
Reviewed-by: andrew
1 parent f11bb89 commit e58378a

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/java.xml/share/classes/com/sun/org/apache/xpath/internal/compiler/Lexer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.sun.org.apache.xml.internal.utils.PrefixResolver;
2525
import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
2626
import java.util.List;
27-
import java.util.Objects;
2827
import javax.xml.transform.TransformerException;
2928
import jdk.xml.internal.XMLSecurityManager;
3029
import jdk.xml.internal.XMLSecurityManager.Limit;
@@ -451,8 +450,7 @@ else if (null != targetStrings)
451450
* @return the next char
452451
*/
453452
private char peekNext(String s, int index) {
454-
Objects.checkIndex(index, s.length());
455-
if (s.length() > index) {
453+
if (index >= 0 && index < s.length() - 1) {
456454
return s.charAt(index + 1);
457455
}
458456
return 0;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2022, SAP SE. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8284548
27+
* @summary Test whether the expected exception is thrown when
28+
* trying to compile an invalid XPath expression.
29+
* @run main InvalidXPath
30+
*/
31+
32+
import javax.xml.xpath.XPathExpressionException;
33+
import javax.xml.xpath.XPathFactory;
34+
35+
public class InvalidXPath {
36+
37+
public static void main(String... args) {
38+
// define an invalid XPath expression
39+
final String invalidXPath = ">>";
40+
41+
// expect XPathExpressionException when the invalid XPath expression is compiled
42+
try {
43+
XPathFactory.newInstance().newXPath().compile(invalidXPath);
44+
} catch (XPathExpressionException e) {
45+
System.out.println("Caught expected exception: " + e.getClass().getName() +
46+
"(" + e.getMessage() + ").");
47+
} catch (Exception e) {
48+
System.out.println("Caught unexpected exception: " + e.getClass().getName() +
49+
"(" + e.getMessage() + ")!");
50+
throw e;
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)