Author: fanningpj
Date: Tue Mar 25 21:12:23 2025
New Revision: 1924601
URL: http://svn.apache.org/viewvc?rev=1924601&view=rev
Log:
[bug-69628] make IOUtils more tolerant of len < 0
Modified:
poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java
poi/trunk/poi/src/test/java/org/apache/poi/util/TestIOUtils.java
Modified: poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java
URL:
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java?rev=1924601&r1=1924600&r2=1924601&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java Tue Mar 25
21:12:23 2025
@@ -248,15 +248,12 @@ public final class IOUtils {
private static byte[] toByteArray(InputStream stream, final int length,
final int maxLength,
final boolean checkEOFException, final
boolean isLengthKnown) throws IOException {
- if (length < 0 || maxLength < 0) {
- throw new RecordFormatException("Can't allocate an array of length
< 0");
- }
final int derivedMaxLength = Math.max(maxLength,
BYTE_ARRAY_MAX_OVERRIDE);
if ((length != Integer.MAX_VALUE) || (derivedMaxLength !=
Integer.MAX_VALUE)) {
checkLength(length, derivedMaxLength);
}
- final int derivedLen = isLengthKnown ? Math.min(length,
derivedMaxLength) : derivedMaxLength;
+ final int derivedLen = isLengthKnown && length >= 0 ? Math.min(length,
derivedMaxLength) : derivedMaxLength;
final int byteArrayInitLen =
calculateByteArrayInitLength(isLengthKnown, length, derivedMaxLength);
final int internalBufferLen = DEFAULT_BUFFER_SIZE;
try (UnsynchronizedByteArrayOutputStream baos =
UnsynchronizedByteArrayOutputStream.builder().setBufferSize(byteArrayInitLen).get())
{
@@ -275,7 +272,7 @@ public final class IOUtils {
throwRecordTruncationException(derivedMaxLength);
}
- if (checkEOFException && derivedLen != Integer.MAX_VALUE &&
totalBytes < derivedLen) {
+ if (checkEOFException && length >= 0 && derivedLen !=
Integer.MAX_VALUE && totalBytes < derivedLen) {
throw new EOFException("unexpected EOF - expected len: " +
derivedLen + " - actual len: " + totalBytes);
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/util/TestIOUtils.java
URL:
http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/util/TestIOUtils.java?rev=1924601&r1=1924600&r2=1924601&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/util/TestIOUtils.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/util/TestIOUtils.java Tue Mar 25
21:12:23 2025
@@ -110,8 +110,25 @@ final class TestIOUtils {
}
@Test
- void testToByteArrayNegativeLength() {
- assertThrows(RecordFormatException.class, () ->
IOUtils.toByteArray(data123(), -1));
+ void testToByteArrayNegativeLength() throws IOException {
+ final byte[] array = new byte[]{1, 2, 3, 4, 5, 6, 7};
+ IOUtils.setByteArrayMaxOverride(30 * 1024 * 1024);
+ try (ByteArrayInputStream is = new ByteArrayInputStream(array)) {
+ assertArrayEquals(array, IOUtils.toByteArray(is, -1, 100));
+ } finally {
+ IOUtils.setByteArrayMaxOverride(-1);
+ }
+ }
+
+ @Test
+ void testToByteArrayNegativeLength2() throws IOException {
+ final byte[] array = new byte[]{1, 2, 3, 4, 5, 6, 7};
+ IOUtils.setByteArrayMaxOverride(30 * 1024 * 1024);
+ try (ByteArrayInputStream is = new ByteArrayInputStream(array)) {
+ assertArrayEquals(array, IOUtils.toByteArray(is, -1));
+ } finally {
+ IOUtils.setByteArrayMaxOverride(-1);
+ }
}
@Test
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]