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 4667226f1bc..6746ede9d9b 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
@@ -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 {
diff --git a/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java b/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java
index a4538bcf5b5..198196a6d13 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 @@ 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 = """
+
+
+
+
+
+
+
+
+
+
+
+ """;
+
+ 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()) {