Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -275,32 +275,18 @@ 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];

staxStreamReader.getTextCharacters(0, chars, 0, textLength);
if (textLength > 0) {
staxStreamReader.getTextCharacters(0, chars, 0, textLength);
}
Comment on lines +281 to +283
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Joe,
The comment above this piece reads:

        // workaround for bugid 5046319 - switch over to commented section
        // below when it is fixed.

And the bug 5046319 (closed issue about AIOOBE) is fixed. Should the commented section be considered?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Naoto,

Yes, I did look through that code and bug 5046319. Unfortunately, it happened when JAXP was standalone, the history of the change was lost.
The variable "buf" seems to be an instance variable that serves to cache the text read. That part had been changed as well. Furthermore, if buf starts with zero length, the call to getTextCharacters would still result in IndexOutOfBoundsException.

Maybe the commented code can be removed, I kept them as is in case they may be useful reference as a history.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, then I'd suggest removing the above comment and commented section altogether, as it only provides confusion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, agree. The code is stable as is.


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 {
Expand Down
54 changes: 53 additions & 1 deletion test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@

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;
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;
Expand All @@ -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
Expand Down Expand Up @@ -139,6 +142,55 @@ public void startElement(String uri, String localName, String qName, Attributes

}

/**
* 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()) {
Expand Down