]> source.dussan.org Git - poi.git/commitdiff
[bug-65639] take BYTE_ARRAY_MAX_OVERRIDE into account in IOUtils#toByteArray
authorPJ Fanning <fanningpj@apache.org>
Sat, 19 Feb 2022 19:35:57 +0000 (19:35 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sat, 19 Feb 2022 19:35:57 +0000 (19:35 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898229 13f79535-47bb-0310-9956-ffa450edef68

poi/src/main/java/org/apache/poi/util/IOUtils.java

index 824ffc44502196bdb5f5c4bc6506ffef0da2c607..d5088334645a1d1fc62a4ffd15c735447a85117c 100644 (file)
@@ -161,8 +161,9 @@ public final class IOUtils {
      * @param length The maximum length to read, use {@link Integer#MAX_VALUE} to read the stream
      *               until EOF
      * @param maxLength if the input is equal to/longer than {@code maxLength} bytes,
- *                   then throw an {@link IOException} complaining about the length.
-*                    use {@link Integer#MAX_VALUE} to disable the check
+     *                  then throw an {@link IOException} complaining about the length.
+     *                  use {@link Integer#MAX_VALUE} to disable the check - if {@link #setByteArrayMaxOverride(int)} is
+     *                  set then that max of that value and this maxLength is used
      * @return A byte array with the read bytes.
      * @throws IOException If reading data fails or EOF is encountered too early for the given length.
      */
@@ -170,14 +171,12 @@ public final class IOUtils {
         if (length < 0L || maxLength < 0L) {
             throw new RecordFormatException("Can't allocate an array of length < 0");
         }
-        // if (length > (long)Integer.MAX_VALUE) {
-        //     throw new RecordFormatException("Can't allocate an array > "+Integer.MAX_VALUE);
-        // }
-        if ((length != Integer.MAX_VALUE) || (maxLength != Integer.MAX_VALUE)) {
-            checkLength(length, maxLength);
+        final int derivedMaxLength = BYTE_ARRAY_MAX_OVERRIDE <= 0 ? maxLength : Math.max(maxLength, BYTE_ARRAY_MAX_OVERRIDE);
+        if ((length != Integer.MAX_VALUE) || (derivedMaxLength != Integer.MAX_VALUE)) {
+            checkLength(length, derivedMaxLength);
         }
 
-        final int len = Math.min(length, maxLength);
+        final int len = Math.min(length, derivedMaxLength);
         try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(len == Integer.MAX_VALUE ? 4096 : len)) {
             byte[] buffer = new byte[4096];
             int totalBytes = 0, readBytes;
@@ -191,8 +190,8 @@ public final class IOUtils {
                 checkByteSizeLimit(totalBytes);
             } while (totalBytes < len && readBytes > -1);
 
-            if (maxLength != Integer.MAX_VALUE && totalBytes == maxLength) {
-                throw new IOException("MaxLength (" + maxLength + ") reached - stream seems to be invalid.");
+            if (derivedMaxLength != Integer.MAX_VALUE && totalBytes == derivedMaxLength) {
+                throw new IOException("MaxLength (" + derivedMaxLength + ") reached - stream seems to be invalid.");
             }
 
             if (len != Integer.MAX_VALUE && totalBytes < len) {