Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8349516

StAXStream2SAX.handleCharacters() fails on empty CDATA

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 25
    • 8, 11, 17, 21, 23, 24, 25
    • xml

      ADDITIONAL SYSTEM INFORMATION :
      // System.getProperty("java.home")
      /usr/lib/jvm/openjdk-21+35_linux-x64

      A DESCRIPTION OF THE PROBLEM :
      In com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.handleCharacters(), text is read using the .getTextLength and .getTextCharacters methods of com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.

      When .getTextLength returns 0, .getTextCharacters errors with IndexOutOfBoundsException. This is triggered by the check targetStart >= target.length where targetStart == 0 and target.length == 0.
       
      Blame can be attributed either to .handleCharacters or to the check of getTextCharacters (I would argue the latter).

      The condition occurs, for example, when StAXStream2SAX.bridge encounters an empty CDATA block.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Call StAXStream2SAX.parse() (or .bridge()) on an XML document containing an empty CDATA block. The code below does this via a Validator.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No error.
      ACTUAL -
      IndexOutOfBoundsException is thrown.

      ---------- BEGIN SOURCE ----------

      import java.io.Reader;
      import java.io.StringReader;

      import javax.xml.XMLConstants;
      import javax.xml.stream.XMLInputFactory;
      import javax.xml.stream.XMLStreamReader;
      import javax.xml.transform.Source;
      import javax.xml.transform.stax.StAXSource;
      import javax.xml.transform.stream.StreamSource;
      import javax.xml.validation.SchemaFactory;
      import javax.xml.validation.Validator;

      public class Test {

      static public void main(String ... args) {

      try {

      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));

      } catch(Exception e) {

      e.printStackTrace();

      }

      }

      }

      ---------- END SOURCE ----------

            joehw Joe Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: