You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2016/11/09 11:15:46 UTC

svn commit: r1768910 - /santuario/xml-security-java/branches/2.0.x-fixes/src/main/java/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java

Author: coheigea
Date: Wed Nov  9 11:15:45 2016
New Revision: 1768910

URL: http://svn.apache.org/viewvc?rev=1768910&view=rev
Log:
SANTUARIO-459 - Wrong usage of Integer.MAX_VALUE

Modified:
    santuario/xml-security-java/branches/2.0.x-fixes/src/main/java/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java

Modified: santuario/xml-security-java/branches/2.0.x-fixes/src/main/java/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/branches/2.0.x-fixes/src/main/java/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java?rev=1768910&r1=1768909&r2=1768910&view=diff
==============================================================================
--- santuario/xml-security-java/branches/2.0.x-fixes/src/main/java/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java (original)
+++ santuario/xml-security-java/branches/2.0.x-fixes/src/main/java/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java Wed Nov  9 11:15:45 2016
@@ -27,6 +27,9 @@ import java.io.OutputStream;
  */
 public class UnsyncByteArrayOutputStream extends OutputStream  {	
 
+    // Maximum array size. Using same value as ArrayList in OpenJDK. 
+    // Integer.MAX_VALUE doesn't work on some VMs, as some header values are reserved
+    private static final int VM_ARRAY_INDEX_MAX_VALUE = Integer.MAX_VALUE - 8;
     private static final int INITIAL_SIZE = 8192;
 
     private byte[] buf;
@@ -38,7 +41,7 @@ public class UnsyncByteArrayOutputStream
     }
 
     public void write(byte[] arg0) {
-        if ((Integer.MAX_VALUE - pos) < arg0.length) {
+        if ((VM_ARRAY_INDEX_MAX_VALUE - pos) < arg0.length) {
             throw new OutOfMemoryError();
         }
         int newPos = pos + arg0.length;
@@ -50,7 +53,7 @@ public class UnsyncByteArrayOutputStream
     }
 
     public void write(byte[] arg0, int arg1, int arg2) {
-        if ((Integer.MAX_VALUE - pos) < arg2) {
+        if ((VM_ARRAY_INDEX_MAX_VALUE - pos) < arg2) {
             throw new OutOfMemoryError();
         }
         int newPos = pos + arg2;
@@ -62,7 +65,7 @@ public class UnsyncByteArrayOutputStream
     }
 
     public void write(int arg0) {
-        if (Integer.MAX_VALUE - pos == 0) {
+        if (VM_ARRAY_INDEX_MAX_VALUE - pos == 0) {
             throw new OutOfMemoryError();
         }
         int newPos = pos + 1;
@@ -81,14 +84,14 @@ public class UnsyncByteArrayOutputStream
     public void reset() {
         pos = 0;
     }
-
+    
     private void expandSize(int newPos) {
         int newSize = size;
         while (newPos > newSize) {
             newSize = newSize << 1;
             // Deal with overflow
             if (newSize < 0) {
-                newSize = Integer.MAX_VALUE;
+                newSize = VM_ARRAY_INDEX_MAX_VALUE;
             }
         }
         byte newBuf[] = new byte[newSize];
@@ -96,4 +99,4 @@ public class UnsyncByteArrayOutputStream
         buf = newBuf;
         size = newSize;
     }
-}
+}
\ No newline at end of file