r27285 - in trunk/freenet: src/freenet/support test/freenet/support

[email protected] Fri, 24 Apr 2009 12:34:47 +0000
Newsgroups gmane.network.freenet.cvs
Message-ID <[email protected]>
Author: j16sdiz
Date: 2009-04-24 12:34:45 +0000 (Fri, 24 Apr 2009)
New Revision: 27285

Modified:
   trunk/freenet/src/freenet/support/Buffer.java
   trunk/freenet/src/freenet/support/ShortBuffer.java
   trunk/freenet/test/freenet/support/BufferTest.java
   trunk/freenet/test/freenet/support/ShortBufferTest.java
Log:
More testing for {Short,}Buffer

Modified: trunk/freenet/src/freenet/support/Buffer.java
===================================================================
--- trunk/freenet/src/freenet/support/Buffer.java	2009-04-24 11:53:37 UTC (rev 27284)
+++ trunk/freenet/src/freenet/support/Buffer.java	2009-04-24 12:34:45 UTC (rev 27285)
@@ -48,8 +48,11 @@
 	 * @throws IOException
 	 */
 	public Buffer(DataInput dis) throws IOException {
-		_data = new byte[dis.readInt()];
-		_length = _data.length;
+		_length = dis.readInt();
+		if(_length < 0)
+			throw new IllegalArgumentException("Negative Length: "+_length);
+
+		_data = new byte[_length];
 		_start = 0;
 		dis.readFully(_data);
 	}
@@ -66,10 +69,10 @@
 	}
 
 	public Buffer(byte[] data, int start, int length) {
+		if(length < 0 || start < 0 || start + length >= data.length)
+		    throw new IllegalArgumentException("Invalid Length: start=" + start + ", length=" + length);
 		_start = start;
 		_data = data;
-		if(length < 0)
-		    throw new IllegalArgumentException();
 		_length = length;
 	}
 

Modified: trunk/freenet/src/freenet/support/ShortBuffer.java
===================================================================
--- trunk/freenet/src/freenet/support/ShortBuffer.java	2009-04-24 11:53:37 UTC (rev 27284)
+++ trunk/freenet/src/freenet/support/ShortBuffer.java	2009-04-24 12:34:45 UTC (rev 27285)
@@ -42,8 +42,11 @@
 	 * @throws IOException
 	 */
 	public ShortBuffer(DataInput dis) throws IOException {
-		_data = new byte[dis.readShort()];
-		_length = (short)_data.length;
+		_length = dis.readShort();
+		if(_length < 0)
+			throw new IllegalArgumentException("Negative Length: "+_length);
+
+		_data = new byte[_length];
 		_start = 0;
 		dis.readFully(_data);
 	}
@@ -54,18 +57,18 @@
 	 * @param data
 	 */
 	public ShortBuffer(byte[] data) {
-		_start = 0;
 		if(data.length > Short.MAX_VALUE)
 		    throw new IllegalArgumentException("Too big: "+data.length);
+		_start = 0;
 		_length = (short)data.length;
 		_data = data;
 	}
 
 	public ShortBuffer(byte[] data, int start, int length) {
+		if(length > Short.MAX_VALUE || length < 0 || start < 0 || start + length >= data.length)
+		    throw new IllegalArgumentException("Invalid Length: start=" + start + ", length=" + length);
 		_start = start;
 		_data = data;
-		if(length > Short.MAX_VALUE || length < 0)
-		    throw new IllegalArgumentException();
 		_length = (short)length;
 	}
 

Modified: trunk/freenet/test/freenet/support/BufferTest.java
===================================================================
--- trunk/freenet/test/freenet/support/BufferTest.java	2009-04-24 11:53:37 UTC (rev 27284)
+++ trunk/freenet/test/freenet/support/BufferTest.java	2009-04-24 12:34:45 UTC (rev 27285)
@@ -61,6 +61,26 @@
 		doTestBuffer(dataSub, buffer);
 	}
 
+	public void testBadLength() {
+		try{
+			new Buffer(new byte[0], 0, -1);
+			fail();
+		} catch(IllegalArgumentException e) {
+			// expect this
+		}
+		try{
+			new Buffer(new byte[0], 0, 1);
+			fail();
+		} catch(IllegalArgumentException e) {
+			// expect this
+		}
+		try{
+			new Buffer(new byte[0], 1, 0);
+			fail();
+		} catch(IllegalArgumentException e) {
+			// expect this
+		}
+	}
 	
 	public void testDataInputStreamBuffer() {
 		

Modified: trunk/freenet/test/freenet/support/ShortBufferTest.java
===================================================================
--- trunk/freenet/test/freenet/support/ShortBufferTest.java	2009-04-24 11:53:37 UTC (rev 27284)
+++ trunk/freenet/test/freenet/support/ShortBufferTest.java	2009-04-24 12:34:45 UTC (rev 27285)
@@ -62,6 +62,32 @@
 		doTestShortBuffer(dataSub, buffer);
 	}
 
+	public void testBadLength() {
+		try{
+			new ShortBuffer(new byte[0], 0, -1);
+			fail();
+		} catch(IllegalArgumentException e) {
+			// expect this
+		}
+		try{
+			new ShortBuffer(new byte[0], 0, 1);
+			fail();
+		} catch(IllegalArgumentException e) {
+			// expect this
+		}
+		try{
+			new ShortBuffer(new byte[0], 1, 0);
+			fail();
+		} catch(IllegalArgumentException e) {
+			// expect this
+		}
+		try{
+			new ShortBuffer(new byte[32768], 0, 32768);
+			fail();
+		} catch(IllegalArgumentException e) {
+			// expect this
+		}
+	}
 	
 	public void testDataInputStreamShortBuffer() {