8349516: StAXStream2SAX.handleCharacters() fails on empty CDATA

Reviewed-by: naoto
This commit is contained in:
Joe Wang 2025-03-04 03:49:17 +00:00
parent 3a8a432c05
commit 96613cc538
2 changed files with 57 additions and 19 deletions

View File

@ -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
@ -275,32 +275,18 @@ public class StAXStream2SAX implements XMLReader, Locator {
}
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];
staxStreamReader.getTextCharacters(0, chars, 0, textLength);
if (textLength > 0) {
staxStreamReader.getTextCharacters(0, chars, 0, textLength);
}
try {
_sax.characters(chars, 0, chars.length);
} 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 {

View File

@ -26,6 +26,8 @@ package validation;
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.XMLStreamReader;
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 @@ import org.xml.sax.helpers.XMLFilterImpl;
/*
* @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,55 @@ public class ValidationTest {
}
/**
* 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}.
*
* 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 {
String schema = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xxxx.com/schema/test"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
>
<xs:element name="test">
<xs:complexType>
<xs:choice>
<xs:element name="tag" type="xs:string" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
""";
String xml = """
<test xmlns="http://xxxx.com/schema/test">
<tag><![CDATA[]]></tag>
</test>
""";
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()) {