diff --git a/src/main/java/org/codehaus/plexus/util/xml/pull/MXParser.java b/src/main/java/org/codehaus/plexus/util/xml/pull/MXParser.java index 38a9ba1a..fc1e4c9d 100644 --- a/src/main/java/org/codehaus/plexus/util/xml/pull/MXParser.java +++ b/src/main/java/org/codehaus/plexus/util/xml/pull/MXParser.java @@ -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; @@ -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); } } diff --git a/src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java b/src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java index 79fb64b1..6ab89978 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java +++ b/src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java @@ -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(""); + sb.append(""); + // 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(""); + + 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 {