commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dam...@apache.org
Subject svn commit: r1209369 - in /commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff: TiffField.java TiffReader.java TiffValueOutsideFileBoundsException.java
Date Fri, 02 Dec 2011 06:36:04 GMT
Author: damjan
Date: Fri Dec  2 06:36:03 2011
New Revision: 1209369

URL: http://svn.apache.org/viewvc?rev=1209369&view=rev
Log:
Rewrite revision 1206412 to throw a helpful exception
in strict mode, and catch and ignore it otherwise.
This is cleaner and lets us reuse that exception
class for other cases.


Added:
    commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffValueOutsideFileBoundsException.java
Modified:
    commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffField.java
    commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffReader.java

Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffField.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffField.java?rev=1209369&r1=1209368&r2=1209369&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffField.java
(original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffField.java
Fri Dec  2 06:36:03 2011
@@ -105,7 +105,7 @@ public class TiffField implements TiffCo
         this.oversizeValue = bytes;
     }
 
-    static FieldType getFieldType(int value)
+    private static FieldType getFieldType(int value)
     {
         for (int i = 0; i < FIELD_TYPES.length; i++)
         {
@@ -262,7 +262,7 @@ public class TiffField implements TiffCo
         return result;
     }
 
-    int getValueLengthInBytes()
+    private int getValueLengthInBytes()
     {
         int unit_length = fieldType.length;
         int valueLength = unit_length * length;
@@ -274,7 +274,7 @@ public class TiffField implements TiffCo
     }
 
     public void fillInValue(ByteSource byteSource) throws ImageReadException,
-            IOException
+            IOException, TiffValueOutsideFileBoundsException
     {
         if (fieldType.isLocalValue(this))
             return;
@@ -286,6 +286,13 @@ public class TiffField implements TiffCo
         // Debug.debug("fillInValue valueOffset", valueOffset);
         // Debug.debug("fillInValue valueLength", valueLength);
 
+        if (valueOffset < 0 ||
+            ((long)valueOffset) + ((long)valueLength) > byteSource.getLength()) {
+            throw new TiffValueOutsideFileBoundsException(
+                "Attempt to read byte range starting from " + valueOffset + " " +
+                "of length " + valueLength + " " +
+                "which is outside the file's size of " + byteSource.getLength());
+        }
         byte bytes[] = byteSource.getBlock(valueOffset, valueLength);
         setOversizeValue(bytes);
     }

Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffReader.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffReader.java?rev=1209369&r1=1209368&r2=1209369&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffReader.java
(original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffReader.java
Fri Dec  2 06:36:03 2011
@@ -189,20 +189,20 @@ public class TiffReader extends BinaryFi
                 // {
                 TiffField field = new TiffField(tag, dirType, type, length,
                         valueOffset, valueOffsetBytes, getByteOrder());
-                FieldType fieldType = TiffField.getFieldType(type);
-                if (!strict &&
-                    !fieldType.isLocalValue(field) &&
-                    (valueOffset < 0 || ((long)valueOffset) + ((long)field.getValueLengthInBytes())
> byteSource.getLength()))
-                {
-                    // corrupt field would throw, ignore it
-                    continue;
-                }
-
                 field.setSortHint(i);
 
                 // Debug.debug("tagInfo", field.tagInfo);
 
-                field.fillInValue(byteSource);
+                try {
+                    field.fillInValue(byteSource);
+                } catch (TiffValueOutsideFileBoundsException valueOutsideFileException) {
+                    if (strict) {
+                        throw new IOException(valueOutsideFileException);
+                    } else {
+                        // corrupt field, ignore it
+                        continue;
+                    }
+                }
 
                 // Debug.debug("\t" + "value", field.getValueDescription());
 

Added: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffValueOutsideFileBoundsException.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffValueOutsideFileBoundsException.java?rev=1209369&view=auto
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffValueOutsideFileBoundsException.java
(added)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffValueOutsideFileBoundsException.java
Fri Dec  2 06:36:03 2011
@@ -0,0 +1,23 @@
+/*
+ * 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
+ *
+ *      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.sanselan.formats.tiff;
+
+public class TiffValueOutsideFileBoundsException extends Exception {
+    public TiffValueOutsideFileBoundsException(String message) {
+        super(message);
+    }
+}



Mime
View raw message