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 2 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 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
17 changes: 17 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,23 @@ public void testSubsequentProcessingInstructionMoreThan8k()
assertEquals( XmlPullParser.END_TAG, parser.nextToken() );
}

@Test
public void testFillBuf_NoOverflow()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you write the test in terms of the public API? Accessing directly the fields and calling directy fillBuf() is not the normal use the parser. I'm thinking generating a big enough XML file, parsing it, showing that fails without the patch and pass the test with the patch in place.

Copy link
Contributor Author

@mealingr mealingr Nov 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you write the test in terms of the public API? Accessing directly the fields and calling directy fillBuf() is not the normal use the parser. I'm thinking generating a big enough XML file, parsing it, showing that fails without the patch and pass the test with the patch in place.

Sure a3f9276 (without the patch that test fails with java.io.IOException: error reading input, returned 0)

throws Exception
{
MXParser parser = new MXParser();
parser.reader = new StringReader("testFillBuf_NoOverflow");
parser.bufEnd = 15941364;
parser.buf = new char[16777216];

parser.fillBuf();

// Without this fix
// https://web.archive.org/web/20070831191548/http://www.extreme.indiana.edu/bugzilla/show_bug.cgi?id=228
// the integer value overflows to -11072962
assertTrue(parser.bufSoftLimit >= 0);
}

public void testMalformedProcessingInstructionAfterTag()
throws Exception
{
Expand Down