You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xmlbeans.apache.org by "Imre Lumiste (JIRA)" <xm...@xml.apache.org> on 2010/04/12 16:18:41 UTC

[jira] Commented: (XMLBEANS-404) entitizeContent CDATA loop iterating too many times (causes assertion error or ArrayIndexOutOfBoundsException in replace)

    [ https://issues.apache.org/jira/browse/XMLBEANS-404?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12855977#action_12855977 ] 

Imre Lumiste commented on XMLBEANS-404:
---------------------------------------

I ran into the same problem -

java.lang.ArrayIndexOutOfBoundsException
	at java.lang.System.arraycopy(Native Method)
	at org.apache.xmlbeans.impl.store.Saver$TextSaver.replace(Saver.java:1608)
	at org.apache.xmlbeans.impl.store.Saver$TextSaver.entitizeContent(Saver.java:1392)
	at org.apache.xmlbeans.impl.store.Saver$TextSaver.emitText(Saver.java:1041)
	at org.apache.xmlbeans.impl.store.Saver.process(Saver.java:309)
	at org.apache.xmlbeans.impl.store.Saver$TextSaver.write(Saver.java:1812)
	at org.apache.xmlbeans.impl.store.Saver$InputStreamSaver.ensure(Saver.java:2515)
	at org.apache.xmlbeans.impl.store.Saver$InputStreamSaver.access$100(Saver.java:2413)
	at org.apache.xmlbeans.impl.store.Saver$InputStreamSaver$OutputStreamImpl.read(Saver.java:2564)
	at org.apache.xmlbeans.impl.store.Saver$InputStreamSaver.read(Saver.java:2498)
	at java.io.InputStream.read(InputStream.java:85)
	at org.apache.xmlbeans.impl.store.Cursor._save(Cursor.java:585)
	at org.apache.xmlbeans.impl.store.Cursor._save(Cursor.java:569)
	at org.apache.xmlbeans.impl.store.Cursor.save(Cursor.java:2526)
	at org.apache.xmlbeans.impl.values.XmlObjectBase.save(XmlObjectBase.java:220)
	at com.example.Example.storeXml(Example.java:231)
...

Or, with assertions enabled:
Exception in thread "main" java.lang.AssertionError
        at org.apache.xmlbeans.impl.store.Saver$TextSaver.replace(Saver.java:1604)
        at org.apache.xmlbeans.impl.store.Saver$TextSaver.entitizeContent(Saver.java:1392)
        at org.apache.xmlbeans.impl.store.Saver$TextSaver.emitText(Saver.java:1041)
        at org.apache.xmlbeans.impl.store.Saver.process(Saver.java:309)
        at org.apache.xmlbeans.impl.store.Saver$TextSaver.write(Saver.java:1812)
        at org.apache.xmlbeans.impl.store.Saver$InputStreamSaver.ensure(Saver.java:2515)
        at org.apache.xmlbeans.impl.store.Saver$InputStreamSaver.access$100(Saver.java:2413)
        at org.apache.xmlbeans.impl.store.Saver$InputStreamSaver$OutputStreamImpl.read(Saver.java:2564)
        at org.apache.xmlbeans.impl.store.Saver$InputStreamSaver.read(Saver.java:2498)
        at java.io.InputStream.read(InputStream.java:85)
        at org.apache.xmlbeans.impl.store.Cursor._save(Cursor.java:585)
        at org.apache.xmlbeans.impl.store.Cursor._save(Cursor.java:569)
        at org.apache.xmlbeans.impl.store.Cursor.save(Cursor.java:2526)
        at org.apache.xmlbeans.impl.values.XmlObjectBase.save(XmlObjectBase.java:220)
        at com.example.Example.storeXml(Example.java:231)
...

 I think a more correct fix for the line (src/store/org/apache/xmlbeans/impl/store/Saver.java:1387 in current trunk) pointed out by original reporter would be

  for ( int cch = _lastEmitCch - 2 ; cch > 0 ; cch-- )

because I figure "cch" stands for "count of characters left to process". In current trunk code it is not taken into account that two characters have already been processed before the loop begins.

I also tried writing a test for this bug, but it's quite hard given that it depends on garbage left in Saver's internal buffer from previous runs. This test, however, would confirm that replacing "]]>" within CDATA works as intended both in the beginning and in the end of CDATA content:

test/src/xmlcursor/checkin/StoreTests.java:1132:
doSaveTest( "<foo><![CDATA[]]]]>><![CDATA[ - that's a small fish <<<<<< foo bar baz and another fish: ]]]]>><![CDATA[]]></foo>" );

Please apply the fix so future versions would not exhibit this bug (it took many hours to sort this out).

> entitizeContent CDATA loop iterating too many times (causes assertion error or ArrayIndexOutOfBoundsException in replace)
> -------------------------------------------------------------------------------------------------------------------------
>
>                 Key: XMLBEANS-404
>                 URL: https://issues.apache.org/jira/browse/XMLBEANS-404
>             Project: XMLBeans
>          Issue Type: Bug
>    Affects Versions: Version 2.4 
>            Reporter: Andrew Parker
>            Priority: Critical
>
> In the entitizeContent routine in Saver.java there is a loop responsible for modifying bad characters and the ">" in "]]>" when surrounding the text with "<![CDATA[" and "]]>".  The loop iterates _lastEmitCch times, but the first two characters of the text have already been processed.
> This causes problems in one of two rare scenarios:
>   The last character in _buf is a ']' and the next two unused characters are ']' and '>' respectively
>   The last two characters in _buf are both ']' and the next unused character is a '>'
> In these instances replace invokes System.arraycopy with invalid parameters as i is outside of the normal _out/_in range resulting in an ArrayIndexOutOfBoundsException or an assertion error is they are enabled.
> In addition, the first two characters are not checked against isBadChar as they're processed outside of this loop.
> I believe that a quick and dirty fix for the ArrayIndexOutOfBoundsException (but not isBadChar) would be to change this particular loop from:
>    for ( int cch = _lastEmitCch ; cch > 0 ; cch-- )
> to 
>   for ( int cch = _lastEmitCch ; cch > 2; cch-- )
> We don't get the isBadChar problem, so I would be grateful if you could confirm the quick and dirty fix above fixes the ArrayIndexOutOfBoundsException as I'd like to patch our production system.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: dev-help@xmlbeans.apache.org