Skip to content

Commit ecdbeeb

Browse files
mealingrhboutemy
authored andcommitted
Add fix for overflow error in MXParser buffer sizing
closes #72
1 parent 905a516 commit ecdbeeb

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/main/java/org/codehaus/plexus/util/xml/pull/MXParser.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,11 @@ protected void ensureEntityCapacity()
401401
protected int bufLoadFactor = 95; // 99%
402402
// protected int bufHardLimit; // only matters when expanding
403403

404-
protected char buf[] = new char[Runtime.getRuntime().freeMemory() > 1000000L ? READ_CHUNK_SIZE : 256];
404+
protected float bufferLoadFactor = bufLoadFactor / 100f;
405405

406-
protected int bufSoftLimit = ( bufLoadFactor * buf.length ) / 100; // desirable size of buffer
406+
protected char buf[] = new char[Runtime.getRuntime().freeMemory() > 1000000L ? READ_CHUNK_SIZE : 256];
407+
408+
protected int bufSoftLimit = (int) ( bufferLoadFactor * buf.length ); // desirable size of buffer
407409

408410
protected boolean preventBufferCompaction;
409411

@@ -3656,7 +3658,8 @@ else if ( expand )
36563658
buf = newBuf;
36573659
if ( bufLoadFactor > 0 )
36583660
{
3659-
bufSoftLimit = ( bufLoadFactor * buf.length ) / 100;
3661+
// Include a fix for https://web.archive.org/web/20070831191548/http://www.extreme.indiana.edu/bugzilla/show_bug.cgi?id=228
3662+
bufSoftLimit = (int) ( bufferLoadFactor * buf.length );
36603663
}
36613664

36623665
}

src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,28 @@ public void testSubsequentProcessingInstructionMoreThan8k()
391391
assertEquals( XmlPullParser.END_TAG, parser.nextToken() );
392392
}
393393

394+
@Test
395+
public void testLargeText_NoOverflow()
396+
throws Exception
397+
{
398+
StringBuffer sb = new StringBuffer();
399+
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
400+
sb.append( "<largetextblock>" );
401+
// Anything above 33,554,431 would fail without a fix for
402+
// https://web.archive.org/web/20070831191548/http://www.extreme.indiana.edu/bugzilla/show_bug.cgi?id=228
403+
// with java.io.IOException: error reading input, returned 0
404+
sb.append( new String( new char[33554432] ) );
405+
sb.append( "</largetextblock>" );
406+
407+
MXParser parser = new MXParser();
408+
parser.setInput( new StringReader( sb.toString() ) );
409+
410+
assertEquals( XmlPullParser.PROCESSING_INSTRUCTION, parser.nextToken() );
411+
assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
412+
assertEquals( XmlPullParser.TEXT, parser.nextToken() );
413+
assertEquals( XmlPullParser.END_TAG, parser.nextToken() );
414+
}
415+
394416
public void testMalformedProcessingInstructionAfterTag()
395417
throws Exception
396418
{

0 commit comments

Comments
 (0)