You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/08/27 20:57:56 UTC

(commons-compress) 04/04: Avoid false positives

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git

commit 87d8e73a09e9b89fa74d537cd793009a5975ad3f
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Wed Aug 27 16:57:46 2025 -0400

    Avoid false positives
    
    Not a reliable check in old VMs or in low memory VMs.
---
 .../commons/compress/archivers/sevenz/SubStreamsInfo.java     | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SubStreamsInfo.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SubStreamsInfo.java
index e1c810507..b29a76c84 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SubStreamsInfo.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SubStreamsInfo.java
@@ -50,14 +50,17 @@ final class SubStreamsInfo {
             // 2 long arrays, just count the longs
             alloc = Math.multiplyExact(intExactCount, Long.BYTES * 2);
             // one BitSet [boolean, long[], int]. just count the long array
-            final int sizeOfBitSet = Math.multiplyExact(Long.BYTES, ((intExactCount - 1) >> 6) + 1);
+            final int sizeOfBitSet = Math.multiplyExact(Long.BYTES, (intExactCount - 1 >> 6) + 1);
             alloc = Math.addExact(alloc, Math.multiplyExact(intExactCount, sizeOfBitSet));
-            // check
-        } catch (ArithmeticException e) {
+        } catch (final ArithmeticException e) {
             throw new CompressException("Cannot create allocation request for a SubStreamsInfo of totalUnpackStreams %,d, maxMemoryLimitKiB %,d: %s",
                     totalUnpackStreams, maxMemoryLimitKiB, e);
         }
-        MemoryLimitException.checkKiB(SevenZFile.bytesToKiB(alloc), maxMemoryLimitKiB);
+        if (Runtime.getRuntime().freeMemory() > 128_000_000L) {
+            // Avoid false positives. 
+            // Not a reliable check in old VMs or in low memory VMs.
+            MemoryLimitException.checkKiB(SevenZFile.bytesToKiB(alloc), maxMemoryLimitKiB);
+        }
         this.hasCrc = new BitSet(intExactCount);
         this.crcs = new long[intExactCount];
         this.unpackSizes = new long[intExactCount];