You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by mu...@apache.org on 2010/10/11 18:32:03 UTC

svn commit: r1021414 - in /santuario/trunk: src/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java src_unitTests/javax/xml/crypto/test/dsig/PhaosXMLDSig3Test.java

Author: mullan
Date: Mon Oct 11 16:32:02 2010
New Revision: 1021414

URL: http://svn.apache.org/viewvc?rev=1021414&view=rev
Log:
Fixed 50050: UnsyncByteArrayOutputStream throws ArrayIndexOutOfBoundsException if array length > internal buffer expansion size

Modified:
    santuario/trunk/src/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java
    santuario/trunk/src_unitTests/javax/xml/crypto/test/dsig/PhaosXMLDSig3Test.java

Modified: santuario/trunk/src/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java
URL: http://svn.apache.org/viewvc/santuario/trunk/src/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java?rev=1021414&r1=1021413&r2=1021414&view=diff
==============================================================================
--- santuario/trunk/src/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java (original)
+++ santuario/trunk/src/org/apache/xml/security/utils/UnsyncByteArrayOutputStream.java Mon Oct 11 16:32:02 2010
@@ -1,5 +1,5 @@
 /*
- * Copyright  1999-2005 The Apache Software Foundation.
+ * 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.
@@ -19,66 +19,71 @@ package org.apache.xml.security.utils;
 import java.io.OutputStream;
 
 /**
- * A simple Unsynced ByteArryOutputStream
+ * A simple Unsynced ByteArrayOutputStream
  * @author raul
  *
  */
 public class UnsyncByteArrayOutputStream extends OutputStream  {	
-	private static ThreadLocal bufCahce = new ThreadLocal() {
+
+    private static final int INITIAL_SIZE = 8192;
+    private static ThreadLocal bufCache = new ThreadLocal() {
         protected synchronized Object initialValue() {
-            return new byte[8*1024];
-        }        
+            return new byte[INITIAL_SIZE];
+        }
     };
-    byte[] buf;
-	int size=8*1024;//buf.length;	
-	int pos=0;
-	/** @inheritDoc */
-	public UnsyncByteArrayOutputStream() {
-		buf=(byte[])bufCahce.get();
-	}
-	public void write(byte[] arg0) {
-		int newPos=pos+arg0.length;
-		if (newPos>size) {
-			expandSize();
-		}
-		System.arraycopy(arg0,0,buf,pos,arg0.length);
-		pos=newPos;
-	}
-	/** @inheritDoc */
-	public void write(byte[] arg0, int arg1, int arg2) {
-		int newPos=pos+arg2;
-		if (newPos>size) {
-			expandSize();
-		}
-		System.arraycopy(arg0,arg1,buf,pos,arg2);
-		pos=newPos;
-	}
-	/** @inheritDoc */
-	public void write(int arg0) {		
-		if (pos>=size) {
-			expandSize();
-		}
-		buf[pos++]=(byte)arg0;		
-	}
-	/** @inheritDoc */
-	public byte[] toByteArray() {
-		byte result[]=new byte[pos];
-		System.arraycopy(buf,0,result,0,pos);
-		return result;
-	}
-	
-	/** @inheritDoc */
-	public void reset() {
-		pos=0;
+
+    private byte[] buf;
+    private int size = INITIAL_SIZE;
+    private int pos = 0;
+
+    public UnsyncByteArrayOutputStream() {
+	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;
+    }
+
+    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;
+    }
+
+    public void write(int arg0) {		
+        int newPos = pos + 1;
+	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;
+    }
+
+    public void reset() {
+	pos = 0;
+    }
 	
-	/** @inheritDoc */
-	void expandSize() {
-		int newSize=size<<2;
-		byte newBuf[]=new byte[newSize];
-		System.arraycopy(buf,0,newBuf,0,pos);		
-		buf=newBuf;
-		size=newSize;
-		
-	}
+    private void expandSize(int newPos) {
+        int newSize = size;
+        while (newPos > newSize) {
+            newSize = newSize<<2;
+        }
+        byte newBuf[] = new byte[newSize];
+        System.arraycopy(buf, 0, newBuf, 0, pos);
+        buf = newBuf;
+        size = newSize;
+    }
 }

Modified: santuario/trunk/src_unitTests/javax/xml/crypto/test/dsig/PhaosXMLDSig3Test.java
URL: http://svn.apache.org/viewvc/santuario/trunk/src_unitTests/javax/xml/crypto/test/dsig/PhaosXMLDSig3Test.java?rev=1021414&r1=1021413&r2=1021414&view=diff
==============================================================================
--- santuario/trunk/src_unitTests/javax/xml/crypto/test/dsig/PhaosXMLDSig3Test.java (original)
+++ santuario/trunk/src_unitTests/javax/xml/crypto/test/dsig/PhaosXMLDSig3Test.java Mon Oct 11 16:32:02 2010
@@ -1,5 +1,5 @@
 /*
- * Copyright 2006-2009 The Apache Software Foundation.
+ * Copyright 2006-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.
@@ -23,6 +23,7 @@ import java.io.File;
 import java.security.Security;
 import javax.xml.crypto.KeySelector;
 import javax.xml.crypto.dsig.XMLSignatureException;
+import javax.xml.crypto.dsig.dom.DOMValidateContext;
 
 import junit.framework.*;
 
@@ -63,8 +64,11 @@ public class PhaosXMLDSig3Test extends T
     public void test_signature_dsa_detached() throws Exception {
 	String file = "signature-dsa-detached.xml";
 
-	boolean coreValidity = 
-	    validator.validate(file, new KeySelectors.RawX509KeySelector());
+        DOMValidateContext vc = validator.getValidateContext
+            (file, new KeySelectors.RawX509KeySelector());
+        vc.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE);
+
+	boolean coreValidity = validator.validate(vc);
 	assertTrue("Signature failed core validation", coreValidity);
     }
     public void test_signature_dsa_enveloped() throws Exception {
@@ -176,8 +180,10 @@ public class PhaosXMLDSig3Test extends T
     public void test_signature_rsa_detached() throws Exception {
 	String file = "signature-rsa-detached.xml";
 
-	boolean coreValidity = 
-	    validator.validate(file, new KeySelectors.RawX509KeySelector());
+        DOMValidateContext vc = validator.getValidateContext
+	    (file, new KeySelectors.RawX509KeySelector());
+        vc.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE);
+	boolean coreValidity = validator.validate(vc);
 	assertTrue("Signature failed core validation", coreValidity);
     }
     public void test_signature_rsa_enveloped_bad_digest_val() throws Exception {