commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject commons-compress git commit: avoid overflow when resizing
Date Fri, 04 Nov 2016 15:50:46 GMT
Repository: commons-compress
Updated Branches:
  refs/heads/master 46f57bf93 -> f538f38bd


avoid overflow when resizing


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/f538f38b
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/f538f38b
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/f538f38b

Branch: refs/heads/master
Commit: f538f38bd4db41349fb8f753ec17850861de5e0e
Parents: 46f57bf
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Fri Nov 4 16:50:23 2016 +0100
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Fri Nov 4 16:50:23 2016 +0100

----------------------------------------------------------------------
 .../utils/SeekableInMemoryByteChannel.java        | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/f538f38b/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
b/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
index 75619b0..eece7f5 100644
--- a/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
+++ b/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
@@ -37,6 +37,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
  */
 public class SeekableInMemoryByteChannel implements SeekableByteChannel {
 
+    private static final int NAIVE_RESIZE_LIMIT = Integer.MAX_VALUE >> 1;
+
     private byte[] data;
     private final AtomicBoolean closed = new AtomicBoolean();
     private int position, size;
@@ -134,7 +136,13 @@ public class SeekableInMemoryByteChannel implements SeekableByteChannel
{
         int wanted = b.remaining();
         int possibleWithoutResize = size - position;
         if (wanted > possibleWithoutResize) {
-            resize(position + wanted);
+            int newSize = position + wanted;
+            if (newSize < 0) { // overflow
+                resize(Integer.MAX_VALUE);
+                wanted = Integer.MAX_VALUE - position;
+            } else {
+                resize(newSize);
+            }
         }
         b.get(data, position, wanted);
         position += wanted;
@@ -162,8 +170,12 @@ public class SeekableInMemoryByteChannel implements SeekableByteChannel
{
         if (len <= 0) {
             len = 1;
         }
-        while (len < newLength) {
-            len <<= 1;
+        if (newLength < NAIVE_RESIZE_LIMIT) {
+            while (len < newLength) {
+                len <<= 1;
+            }
+        } else { // avoid overflow
+            len = newLength;
         }
         data = Arrays.copyOf(data, len);
     }


Mime
View raw message