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 2012/07/31 13:50:04 UTC

svn commit: r1367505 - in /santuario/xml-security-java/branches/1.4.x-fixes: CHANGELOG.txt src/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java

Author: coheigea
Date: Tue Jul 31 11:50:03 2012
New Revision: 1367505

URL: http://svn.apache.org/viewvc?rev=1367505&view=rev
Log:
[SANTUARIO-334] - UnsyncByteArrayOutputStream hangs on messages larger 512 MB

Modified:
    santuario/xml-security-java/branches/1.4.x-fixes/CHANGELOG.txt
    santuario/xml-security-java/branches/1.4.x-fixes/src/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java

Modified: santuario/xml-security-java/branches/1.4.x-fixes/CHANGELOG.txt
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/branches/1.4.x-fixes/CHANGELOG.txt?rev=1367505&r1=1367504&r2=1367505&view=diff
==============================================================================
--- santuario/xml-security-java/branches/1.4.x-fixes/CHANGELOG.txt (original)
+++ santuario/xml-security-java/branches/1.4.x-fixes/CHANGELOG.txt Tue Jul 31 11:50:03 2012
@@ -1,6 +1,7 @@
 Changelog for "Apache xml-security" <http://santuario.apache.org/>
 
 New in v.1.4.8-SNAPSHOT:
+    Fixed SANTUARIO-334 - UnsyncByteArrayOutputStream hangs on messages larger 512 MB.
 
 New in v.1.4.7:
     Fixed SANTUARIO-306 - KeySelectors loop

Modified: santuario/xml-security-java/branches/1.4.x-fixes/src/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/branches/1.4.x-fixes/src/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java?rev=1367505&r1=1367504&r2=1367505&view=diff
==============================================================================
--- santuario/xml-security-java/branches/1.4.x-fixes/src/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java (original)
+++ santuario/xml-security-java/branches/1.4.x-fixes/src/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java Tue Jul 31 11:50:03 2012
@@ -1,21 +1,24 @@
-/*
- * Copyright 1999-2010 The Apache Software Foundation.
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
  *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
  */
 package org.apache.xml.security.utils;
 
+import java.io.IOException;
 import java.io.OutputStream;
 
 /**
@@ -37,49 +40,62 @@ public class UnsyncByteArrayOutputStream
     private int pos = 0;
 
     public UnsyncByteArrayOutputStream() {
-	buf = (byte[])bufCache.get();
+        buf = (byte[])bufCache.get();
     }
 
     public void write(byte[] arg0) {
-	int newPos = pos + arg0.length;
-	if (newPos > size) {
-	    expandSize(newPos);
-	}
-	System.arraycopy(arg0, 0, buf, pos, arg0.length);
-	pos = newPos;
+        if ((Integer.MAX_VALUE - pos) < arg0.length) {
+            throw new OutOfMemoryError();
+        }
+        int newPos = pos + arg0.length;
+        if (newPos > size) {
+            expandSize(newPos);
+        }
+        System.arraycopy(arg0, 0, buf, pos, arg0.length);
+        pos = newPos;
     }
 
     public void write(byte[] arg0, int arg1, int arg2) {
-	int newPos = pos + arg2;
-	if (newPos > size) {
-	    expandSize(newPos);
-	}
-	System.arraycopy(arg0, arg1, buf, pos, arg2);
-	pos = newPos;
+        if ((Integer.MAX_VALUE - pos) < arg2) {
+            throw new OutOfMemoryError();
+        }
+        int newPos = pos + arg2;
+        if (newPos > size) {
+            expandSize(newPos);
+        }
+        System.arraycopy(arg0, arg1, buf, pos, arg2);
+        pos = newPos;
     }
 
-    public void write(int arg0) {		
+    public void write(int arg0) {
+        if ((Integer.MAX_VALUE - pos) == 0) {
+            throw new OutOfMemoryError();
+        }
         int newPos = pos + 1;
-	if (newPos > size) {
-	    expandSize(newPos);
-	}
-	buf[pos++] = (byte)arg0;		
+        if (newPos > size) {
+            expandSize(newPos);
+        }
+        buf[pos++] = (byte)arg0;		
     }
 
     public byte[] toByteArray() {
-	byte result[] = new byte[pos];
-	System.arraycopy(buf, 0, result, 0, pos);
-	return result;
+        byte result[] = new byte[pos];
+        System.arraycopy(buf, 0, result, 0, pos);
+        return result;
     }
 
     public void reset() {
-	pos = 0;
+        pos = 0;
     }
-	
+    
     private void expandSize(int newPos) {
         int newSize = size;
         while (newPos > newSize) {
-            newSize = newSize<<2;
+            newSize = newSize << 1;
+            // Deal with overflow
+            if (newSize < 0) {
+                newSize = Integer.MAX_VALUE;
+            }
         }
         byte newBuf[] = new byte[newSize];
         System.arraycopy(buf, 0, newBuf, 0, pos);