From 70fc2ca599f6ea15522ecc75790e9df86e89d28a Mon Sep 17 00:00:00 2001 From: JoeWang-Java Date: Fri, 28 Feb 2025 17:40:03 +0000 Subject: [PATCH 1/3] 8349516: StAXStream2SAX.handleCharacters() fails on empty CDATA --- .../internal/xsltc/trax/StAXStream2SAX.java | 6 ++- .../unittest/validation/ValidationTest.java | 51 ++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/StAXStream2SAX.java b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/StAXStream2SAX.java index 4667226f1bc0b..dc160aa457608 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/StAXStream2SAX.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/StAXStream2SAX.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2025, Oracle and/or its affiliates. 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 @@ -281,7 +281,9 @@ private void handleCharacters() throws XMLStreamException { int textLength = staxStreamReader.getTextLength(); char[] chars = new char[textLength]; - staxStreamReader.getTextCharacters(0, chars, 0, textLength); + if (textLength > 0) { + staxStreamReader.getTextCharacters(0, chars, 0, textLength); + } try { _sax.characters(chars, 0, chars.length); diff --git a/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java b/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java index a4538bcf5b501..2d61abe8415a2 100644 --- a/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java +++ b/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java @@ -26,6 +26,8 @@ import java.io.File; import java.io.FileInputStream; +import java.io.Reader; +import java.io.StringReader; import javax.xml.XMLConstants; import javax.xml.parsers.SAXParserFactory; import javax.xml.stream.XMLInputFactory; @@ -33,6 +35,7 @@ import javax.xml.stream.events.XMLEvent; import javax.xml.transform.Source; import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.stax.StAXSource; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; @@ -48,7 +51,7 @@ /* * @test - * @bug 8220818 8176447 + * @bug 8220818 8176447 8349516 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest * @run testng/othervm validation.ValidationTest * @summary Runs validations with schemas and sources @@ -139,6 +142,52 @@ public void startElement(String uri, String localName, String qName, Attributes } + /** + * Verifies the bug fix for 8349516. The fix adds a guard against empty text + * since calling StreamReader.getTextCharacters with textLength=0 will result + * in IndexOutOfBoundsException. + * + * @throws Exception if the test fails, in which case the parser throws + * IndexOutOfBoundsException. + */ + @Test + public void testValidationWithStAX() throws Exception { + String schema = """ + + + + + + + + + + + + """; + + String xml = """ + + + + """; + + Reader schemaReader = new StringReader(schema); + Reader xmlReader = new StringReader(xml); + + Source source = new StreamSource(schemaReader); + + Validator validator = + SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(source).newValidator(); + + XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(xmlReader); + validator.validate(new StAXSource(xmlStreamReader)); + } + private static String getTargetNamespace(String xsdFile) throws Exception { XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(xsdFile)); while (reader.hasNext()) { From 2d4b7653b828b6daaa501784e097ea779768f960 Mon Sep 17 00:00:00 2001 From: JoeWang-Java Date: Sat, 1 Mar 2025 01:16:11 +0000 Subject: [PATCH 2/3] remove commented section --- .../internal/xsltc/trax/StAXStream2SAX.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/StAXStream2SAX.java b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/StAXStream2SAX.java index dc160aa457608..6746ede9d9b54 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/StAXStream2SAX.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/StAXStream2SAX.java @@ -275,9 +275,6 @@ private void handlePI() throws XMLStreamException { } private void handleCharacters() throws XMLStreamException { - - // workaround for bugid 5046319 - switch over to commented section - // below when it is fixed. int textLength = staxStreamReader.getTextLength(); char[] chars = new char[textLength]; @@ -290,19 +287,6 @@ private void handleCharacters() throws XMLStreamException { } catch (SAXException e) { throw new XMLStreamException(e); } - - -// int start = 0; -// int len; -// do { -// len = staxStreamReader.getTextCharacters(start, buf, 0, buf.length); -// start += len; -// try { -// _sax.characters(buf, 0, len); -// } catch (SAXException e) { -// throw new XMLStreamException(e); -// } -// } while (len == buf.length); } private void handleEndElement() throws XMLStreamException { From c8f07127bf161fcf10946dbe75af65128dcd4c8e Mon Sep 17 00:00:00 2001 From: JoeWang-Java Date: Mon, 3 Mar 2025 19:19:58 +0000 Subject: [PATCH 3/3] improve the javadoc for the test --- .../jaxp/unittest/validation/ValidationTest.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java b/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java index 2d61abe8415a2..198196a6d1371 100644 --- a/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java +++ b/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java @@ -143,12 +143,15 @@ public void startElement(String uri, String localName, String qName, Attributes } /** - * Verifies the bug fix for 8349516. The fix adds a guard against empty text - * since calling StreamReader.getTextCharacters with textLength=0 will result - * in IndexOutOfBoundsException. + * Verifies the bug fix for 8349516, which adds a guard against empty text. + * Prior to the fix, calling {@link XMLStreamReader#getTextCharacters() XMLStreamReader#getTextCharacters()} + * with {@code length = 0} resulted in an {@code IndexOutOfBoundsException}. * - * @throws Exception if the test fails, in which case the parser throws - * IndexOutOfBoundsException. + * This test ensures that the fix prevents such an exception. + * + * @throws Exception if the test fails due to unexpected issues, such as errors + * in creating the schema or reader, or validation errors other than the + * {@code IndexOutOfBoundsException}. */ @Test public void testValidationWithStAX() throws Exception {