Skip to content

Commit 70fc2ca

Browse files
committed
8349516: StAXStream2SAX.handleCharacters() fails on empty CDATA
1 parent e98df71 commit 70fc2ca

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/xalan/internal/xsltc/trax/StAXStream2SAX.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -281,7 +281,9 @@ private void handleCharacters() throws XMLStreamException {
281281
int textLength = staxStreamReader.getTextLength();
282282
char[] chars = new char[textLength];
283283

284-
staxStreamReader.getTextCharacters(0, chars, 0, textLength);
284+
if (textLength > 0) {
285+
staxStreamReader.getTextCharacters(0, chars, 0, textLength);
286+
}
285287

286288
try {
287289
_sax.characters(chars, 0, chars.length);

test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626

2727
import java.io.File;
2828
import java.io.FileInputStream;
29+
import java.io.Reader;
30+
import java.io.StringReader;
2931
import javax.xml.XMLConstants;
3032
import javax.xml.parsers.SAXParserFactory;
3133
import javax.xml.stream.XMLInputFactory;
3234
import javax.xml.stream.XMLStreamReader;
3335
import javax.xml.stream.events.XMLEvent;
3436
import javax.xml.transform.Source;
3537
import javax.xml.transform.sax.SAXSource;
38+
import javax.xml.transform.stax.StAXSource;
3639
import javax.xml.transform.stream.StreamSource;
3740
import javax.xml.validation.Schema;
3841
import javax.xml.validation.SchemaFactory;
@@ -48,7 +51,7 @@
4851

4952
/*
5053
* @test
51-
* @bug 8220818 8176447
54+
* @bug 8220818 8176447 8349516
5255
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
5356
* @run testng/othervm validation.ValidationTest
5457
* @summary Runs validations with schemas and sources
@@ -139,6 +142,52 @@ public void startElement(String uri, String localName, String qName, Attributes
139142

140143
}
141144

145+
/**
146+
* Verifies the bug fix for 8349516. The fix adds a guard against empty text
147+
* since calling StreamReader.getTextCharacters with textLength=0 will result
148+
* in IndexOutOfBoundsException.
149+
*
150+
* @throws Exception if the test fails, in which case the parser throws
151+
* IndexOutOfBoundsException.
152+
*/
153+
@Test
154+
public void testValidationWithStAX() throws Exception {
155+
String schema = """
156+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
157+
targetNamespace="http://xxxx.com/schema/test"
158+
attributeFormDefault="unqualified"
159+
elementFormDefault="qualified"
160+
>
161+
162+
<xs:element name="test">
163+
<xs:complexType>
164+
<xs:choice>
165+
<xs:element name="tag" type="xs:string" />
166+
</xs:choice>
167+
</xs:complexType>
168+
</xs:element>
169+
170+
</xs:schema>
171+
""";
172+
173+
String xml = """
174+
<test xmlns="http://xxxx.com/schema/test">
175+
<tag><![CDATA[]]></tag>
176+
</test>
177+
""";
178+
179+
Reader schemaReader = new StringReader(schema);
180+
Reader xmlReader = new StringReader(xml);
181+
182+
Source source = new StreamSource(schemaReader);
183+
184+
Validator validator =
185+
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(source).newValidator();
186+
187+
XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(xmlReader);
188+
validator.validate(new StAXSource(xmlStreamReader));
189+
}
190+
142191
private static String getTargetNamespace(String xsdFile) throws Exception {
143192
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(xsdFile));
144193
while (reader.hasNext()) {

0 commit comments

Comments
 (0)