Uploaded image for project: 'XMLBeans'
  1. XMLBeans
  2. XMLBEANS-604

Incorrect Assert in Saver.TextSaver

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • Version 5.0.3
    • Version 5.1.0
    • XmlObject
    • None
    • Windows 10, JDK 1.8.0_311

    Description

      If the Buffer _buf in org.apache.xmlbeans.impl.store.Saver.TextSaver.write(Writer, int) is completely filled (this happens if the current charsAvailable is a large power of 2, in the example 2^14=16384), the _in index is set to 0 ( = 16384 % 16384) in emit(String). This causes the assertion

       

      assert _free == _buf.length - _in;

      to fail (with _free = 0, _buf.length = 16384, _in = 0). The behaviour of the code seems correct, but the assert does not account for the fact that _in can wrap around to 0.

       

      The following code reproduces the problem (if assertions are enabled):

       

      import org.apache.xmlbeans.XmlBeans;
      import org.apache.xmlbeans.XmlObject;
      
      import java.io.InputStream;
      
      public class AssertionTest {
        public static void main(String[] args) throws Exception {
          System.getProperties().setProperty("line.separator", "\n");
          String xml = "<test>" + new String(new char[16339]).replace('\0', 'x') + "</test>";
          XmlObject object = XmlObject.Factory.parse(xml);
          try (InputStream is = object.newInputStream()) {
            is.read(new byte[8192], 0, 8192);
          }
        }
      }

      with the following stack trace:

       

       

      Exception in thread "main" java.lang.AssertionError
          at org.apache.xmlbeans.impl.store.Saver$TextSaver.write(Saver.java:1708)
          at org.apache.xmlbeans.impl.store.Saver$InputStreamSaver.ensure(Saver.java:2344)
          at org.apache.xmlbeans.impl.store.Saver$InputStreamSaver.access$200(Saver.java:2230)
          at org.apache.xmlbeans.impl.store.Saver$InputStreamSaver$OutputStreamImpl.read(Saver.java:2389)
          at org.apache.xmlbeans.impl.store.Saver$InputStreamSaver.lambda$read$0(Saver.java:2328)
          at org.apache.xmlbeans.impl.store.Saver.syncWrap(Saver.java:2180)
          at org.apache.xmlbeans.impl.store.Saver.access$000(Saver.java:33)
          at org.apache.xmlbeans.impl.store.Saver$InputStreamSaver.read(Saver.java:2328)
          at AssertionTest.main(AssertionTest.java:12)

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            timon.hertli Timon Hertli
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 20m
                20m