Author: cmchen
Date: Thu Sep  9 18:09:20 2010
New Revision: 995526

URL: http://svn.apache.org/viewvc?rev=995526&view=rev
Log:
Added checks in ByteSource's getBlock() methods for invalid params.

Jira issue keys: SANSELAN-39 , SANSELAN-17

Modified:
    
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceArray.java
    
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceFile.java
    
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceInputStream.java

Modified: 
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceArray.java
URL: 
http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceArray.java?rev=995526&r1=995525&r2=995526&view=diff
==============================================================================
--- 
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceArray.java
 (original)
+++ 
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceArray.java
 Thu Sep  9 18:09:20 2010
@@ -36,17 +36,19 @@ public class ByteSourceArray extends Byt
                this.bytes = bytes;
        }
 
-       public InputStream getInputStream() //throws IOException
+       public InputStream getInputStream()
        {
                return new ByteArrayInputStream(bytes);
        }
 
        public byte[] getBlock(int start, int length) throws IOException
        {
-               if (start + length > bytes.length)
+               // We include a separate check for int overflow.
+               if ((start < 0) || (length < 0) || (start + length < 0) || 
(start + length > bytes.length)) {
                        throw new IOException("Could not read block (block 
start: " + start
                                        + ", block length: " + length + ", data 
length: "
                                        + bytes.length + ").");
+               }
 
                byte result[] = new byte[length];
                System.arraycopy(bytes, start, result, 0, length);

Modified: 
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceFile.java
URL: 
http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceFile.java?rev=995526&r1=995525&r2=995526&view=diff
==============================================================================
--- 
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceFile.java
 (original)
+++ 
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceFile.java
 Thu Sep  9 18:09:20 2010
@@ -47,10 +47,18 @@ public class ByteSourceFile extends Byte
 
        public byte[] getBlock(int start, int length) throws IOException
        {
+               
                RandomAccessFile raf = null;
                try
                {
                        raf = new RandomAccessFile(file, "r");
+                       
+                       // We include a separate check for int overflow.
+                       if ((start < 0) || (length < 0) || (start + length < 0) 
|| (start + length > raf.length())) {
+                               throw new IOException("Could not read block 
(block start: " + start
+                                               + ", block length: " + length + 
", data length: "
+                                               + raf.length() + ").");
+                       }
 
                        return getRAFBytes(raf, start, length,
                                        "Could not read value from file");

Modified: 
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceInputStream.java
URL: 
http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceInputStream.java?rev=995526&r1=995525&r2=995526&view=diff
==============================================================================
--- 
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceInputStream.java
 (original)
+++ 
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceInputStream.java
 Thu Sep  9 18:09:20 2010
@@ -167,12 +167,22 @@ public class ByteSourceInputStream exten
                return new CacheReadingInputStream();
        }
 
-       public byte[] getBlock(int start, int length) throws IOException
+       public byte[] getBlock(int blockStart, int blockLength) throws 
IOException
        {
+               // We include a separate check for int overflow.
+               if ((blockStart < 0)
+                               || (blockLength < 0)
+                               || (blockStart + blockLength < 0)
+                               || (blockStart + blockLength > 
streamLength.longValue())) {
+                       throw new IOException("Could not read block (block 
start: " + blockStart
+                                       + ", block length: " + blockLength + ", 
data length: "
+                                       + streamLength + ").");
+               }
+
                InputStream is = getInputStream();
-               is.skip(start);
+               is.skip(blockStart);
 
-               byte bytes[] = new byte[length];
+               byte bytes[] = new byte[blockLength];
                int total = 0;
                while (true)
                {
@@ -180,24 +190,24 @@ public class ByteSourceInputStream exten
                        if (read < 1)
                                throw new IOException("Could not read block.");
                        total += read;
-                       if (total >= length)
+                       if (total >= blockLength)
                                return bytes;
                }
        }
 
-       private Long length = null;
+       private Long streamLength = null;
 
        public long getLength() throws IOException
        {
-               if (length != null)
-                       return length.longValue();
+               if (streamLength != null)
+                       return streamLength.longValue();
 
                InputStream is = getInputStream();
                long result = 0;
                long skipped;
                while ((skipped = is.skip(1024)) > 0)
                        result += skipped;
-               length = new Long(result);
+               streamLength = new Long(result);
                return result;
        }
 


Reply via email to