aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Langer <clanger@openjdk.org>2022-04-08 14:06:47 +0200
committerChristoph Langer <clanger@openjdk.org>2022-04-09 14:16:14 +0200
commite58378a58e91bd10fc60031148b558230d43686f (patch)
tree2319776c198fd96b19d3e88812a3f9833becc9b5
parentf11bb8984e92400b0d42935c251e95c65e04dedb (diff)
downloadlibcore-e58378a58e91bd10fc60031148b558230d43686f.tar.gz
8284548: Invalid XPath expression causes StringIndexOutOfBoundsExceptionjdk17u/jdk-17.0.3+6
Reviewed-by: andrew
-rw-r--r--src/java.xml/share/classes/com/sun/org/apache/xpath/internal/compiler/Lexer.java4
-rw-r--r--test/jdk/javax/xml/jaxp/XPath/InvalidXPath.java53
2 files changed, 54 insertions, 3 deletions
diff --git a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/compiler/Lexer.java b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/compiler/Lexer.java
index 54595e2d036..b7b3f419eb2 100644
--- a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/compiler/Lexer.java
+++ b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/compiler/Lexer.java
@@ -24,7 +24,6 @@ import com.sun.org.apache.xalan.internal.res.XSLMessages;
import com.sun.org.apache.xml.internal.utils.PrefixResolver;
import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
import java.util.List;
-import java.util.Objects;
import javax.xml.transform.TransformerException;
import jdk.xml.internal.XMLSecurityManager;
import jdk.xml.internal.XMLSecurityManager.Limit;
@@ -451,8 +450,7 @@ class Lexer
* @return the next char
*/
private char peekNext(String s, int index) {
- Objects.checkIndex(index, s.length());
- if (s.length() > index) {
+ if (index >= 0 && index < s.length() - 1) {
return s.charAt(index + 1);
}
return 0;
diff --git a/test/jdk/javax/xml/jaxp/XPath/InvalidXPath.java b/test/jdk/javax/xml/jaxp/XPath/InvalidXPath.java
new file mode 100644
index 00000000000..478f4212d5b
--- /dev/null
+++ b/test/jdk/javax/xml/jaxp/XPath/InvalidXPath.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2022, SAP SE. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8284548
+ * @summary Test whether the expected exception is thrown when
+ * trying to compile an invalid XPath expression.
+ * @run main InvalidXPath
+ */
+
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+public class InvalidXPath {
+
+ public static void main(String... args) {
+ // define an invalid XPath expression
+ final String invalidXPath = ">>";
+
+ // expect XPathExpressionException when the invalid XPath expression is compiled
+ try {
+ XPathFactory.newInstance().newXPath().compile(invalidXPath);
+ } catch (XPathExpressionException e) {
+ System.out.println("Caught expected exception: " + e.getClass().getName() +
+ "(" + e.getMessage() + ").");
+ } catch (Exception e) {
+ System.out.println("Caught unexpected exception: " + e.getClass().getName() +
+ "(" + e.getMessage() + ")!");
+ throw e;
+ }
+ }
+}