Skip to content

Add fix for overflow error in MXParser buffer sizing #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/main/java/org/codehaus/plexus/util/xml/pull/MXParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,11 @@ protected void ensureEntityCapacity()
protected int bufLoadFactor = 95; // 99%
// protected int bufHardLimit; // only matters when expanding

protected float bufferLoadFactor = bufLoadFactor / 100f;

protected char buf[] = new char[Runtime.getRuntime().freeMemory() > 1000000L ? READ_CHUNK_SIZE : 256];

protected int bufSoftLimit = ( bufLoadFactor * buf.length ) / 100; // desirable size of buffer
protected int bufSoftLimit = (int) (bufferLoadFactor * buf.length); // desirable size of buffer

protected boolean preventBufferCompaction;

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

}
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,28 @@ public void testSubsequentProcessingInstructionMoreThan8k()
assertEquals( XmlPullParser.END_TAG, parser.nextToken() );
}

@Test
public void testLargeText_NoOverflow()
throws Exception
{
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<largetextblock>");
// Anything above 33,554,431 would fail without a fix for
// https://web.archive.org/web/20070831191548/http://www.extreme.indiana.edu/bugzilla/show_bug.cgi?id=228
// with java.io.IOException: error reading input, returned 0
sb.append(new String(new char[33554432]));
sb.append("</largetextblock>");

MXParser parser = new MXParser();
parser.setInput(new StringReader(sb.toString()));

assertEquals(XmlPullParser.PROCESSING_INSTRUCTION, parser.nextToken());
assertEquals(XmlPullParser.START_TAG, parser.nextToken());
assertEquals(XmlPullParser.TEXT, parser.nextToken());
assertEquals(XmlPullParser.END_TAG, parser.nextToken());
}

public void testMalformedProcessingInstructionAfterTag()
throws Exception
{
Expand Down