Skip to content

Commit 96613cc

Browse files
committed
8349516: StAXStream2SAX.handleCharacters() fails on empty CDATA
Reviewed-by: naoto
1 parent 3a8a432 commit 96613cc

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/StAXStream2SAX.java

Lines changed: 4 additions & 18 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
@@ -275,32 +275,18 @@ private void handlePI() throws XMLStreamException {
275275
}
276276

277277
private void handleCharacters() throws XMLStreamException {
278-
279-
// workaround for bugid 5046319 - switch over to commented section
280-
// below when it is fixed.
281278
int textLength = staxStreamReader.getTextLength();
282279
char[] chars = new char[textLength];
283280

284-
staxStreamReader.getTextCharacters(0, chars, 0, textLength);
281+
if (textLength > 0) {
282+
staxStreamReader.getTextCharacters(0, chars, 0, textLength);
283+
}
285284

286285
try {
287286
_sax.characters(chars, 0, chars.length);
288287
} catch (SAXException e) {
289288
throw new XMLStreamException(e);
290289
}
291-
292-
293-
// int start = 0;
294-
// int len;
295-
// do {
296-
// len = staxStreamReader.getTextCharacters(start, buf, 0, buf.length);
297-
// start += len;
298-
// try {
299-
// _sax.characters(buf, 0, len);
300-
// } catch (SAXException e) {
301-
// throw new XMLStreamException(e);
302-
// }
303-
// } while (len == buf.length);
304290
}
305291

306292
private void handleEndElement() throws XMLStreamException {

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

Lines changed: 53 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,55 @@ public void startElement(String uri, String localName, String qName, Attributes
139142

140143
}
141144

145+
/**
146+
* Verifies the bug fix for 8349516, which adds a guard against empty text.
147+
* Prior to the fix, calling {@link XMLStreamReader#getTextCharacters() XMLStreamReader#getTextCharacters()}
148+
* with {@code length = 0} resulted in an {@code IndexOutOfBoundsException}.
149+
*
150+
* This test ensures that the fix prevents such an exception.
151+
*
152+
* @throws Exception if the test fails due to unexpected issues, such as errors
153+
* in creating the schema or reader, or validation errors other than the
154+
* {@code IndexOutOfBoundsException}.
155+
*/
156+
@Test
157+
public void testValidationWithStAX() throws Exception {
158+
String schema = """
159+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
160+
targetNamespace="http://xxxx.com/schema/test"
161+
attributeFormDefault="unqualified"
162+
elementFormDefault="qualified"
163+
>
164+
165+
<xs:element name="test">
166+
<xs:complexType>
167+
<xs:choice>
168+
<xs:element name="tag" type="xs:string" />
169+
</xs:choice>
170+
</xs:complexType>
171+
</xs:element>
172+
173+
</xs:schema>
174+
""";
175+
176+
String xml = """
177+
<test xmlns="http://xxxx.com/schema/test">
178+
<tag><![CDATA[]]></tag>
179+
</test>
180+
""";
181+
182+
Reader schemaReader = new StringReader(schema);
183+
Reader xmlReader = new StringReader(xml);
184+
185+
Source source = new StreamSource(schemaReader);
186+
187+
Validator validator =
188+
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(source).newValidator();
189+
190+
XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(xmlReader);
191+
validator.validate(new StAXSource(xmlStreamReader));
192+
}
193+
142194
private static String getTargetNamespace(String xsdFile) throws Exception {
143195
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(xsdFile));
144196
while (reader.hasNext()) {

0 commit comments

Comments
 (0)