Skip to content

Commit 994f2e9

Browse files
committed
8284548: Invalid XPath expression causes StringIndexOutOfBoundsException
Reviewed-by: naoto, lancea
1 parent 691c5da commit 994f2e9

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

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

Lines changed: 2 additions & 4 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;
@@ -33,7 +32,7 @@
3332
* This class is in charge of lexical processing of the XPath
3433
* expression into tokens.
3534
*
36-
* @LastModified: Jan 2022
35+
* @LastModified: Apr 2022
3736
*/
3837
class Lexer
3938
{
@@ -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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. 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+
package xpath;
25+
26+
import java.io.StringReader;
27+
import javax.xml.parsers.DocumentBuilder;
28+
import javax.xml.parsers.DocumentBuilderFactory;
29+
import javax.xml.xpath.XPath;
30+
import javax.xml.xpath.XPathExpression;
31+
import javax.xml.xpath.XPathExpressionException;
32+
import javax.xml.xpath.XPathFactory;
33+
import org.testng.Assert;
34+
import org.testng.annotations.DataProvider;
35+
import org.testng.annotations.Test;
36+
import org.w3c.dom.Document;
37+
import org.w3c.dom.Node;
38+
39+
/*
40+
* @test
41+
* @bug 8284548
42+
* @run testng xpath.XPathExceptionTest
43+
* @summary This is a general test for Exception handling. Additional cases may
44+
* be added with a bug id in the test cases.
45+
*/
46+
public class XPathExceptionTest {
47+
48+
/*
49+
* DataProvider: invalid XPath expressions
50+
* Illegal expressions and structures that may escape the validation check.
51+
*/
52+
@DataProvider(name = "invalid")
53+
public Object[][] getInvalid() throws Exception {
54+
return new Object[][]{
55+
// @bug JDK-8284548: expressions ending with relational operators
56+
// throw StringIndexOutOfBoundsException instead of XPathExpressionException
57+
{"/a/b/c[@d >"},
58+
{"/a/b/c[@d <"},
59+
{"/a/b/c[@d >="},
60+
{">>"},
61+
};
62+
}
63+
64+
/**
65+
* Verifies that the XPath processor throws XPathExpressionException upon
66+
* encountering illegal XPath expressions.
67+
* @param invalidExp an illegal XPath expression
68+
* @throws Exception if the test fails
69+
*/
70+
@Test(dataProvider = "invalid")
71+
public void testIllegalExp(String invalidExp) throws Exception {
72+
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
73+
Document doc = builder.parse(new org.xml.sax.InputSource(new StringReader("<A/>")));
74+
Assert.assertThrows(XPathExpressionException.class, () -> evaluate(doc, invalidExp));
75+
}
76+
77+
private void evaluate(Document doc, String s) throws XPathExpressionException {
78+
XPath xp = XPathFactory.newInstance().newXPath();
79+
XPathExpression xe = xp.compile(s);
80+
xe.evaluateExpression(doc, Node.class);
81+
}
82+
}

0 commit comments

Comments
 (0)