sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1773451 - in /sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis: internal/geotiff/Resources.java internal/geotiff/Resources.properties internal/geotiff/Resources_fr.properties storage/geotiff/Reader.java
Date Fri, 09 Dec 2016 18:41:04 GMT
Author: desruisseaux
Date: Fri Dec  9 18:41:04 2016
New Revision: 1773451

URL: http://svn.apache.org/viewvc?rev=1773451&view=rev
Log:
When reading a TIFF file, apply a safety check against circular references in the IFD chain.

Modified:
    sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources.java
    sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources.properties
    sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources_fr.properties
    sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Reader.java

Modified: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources.java?rev=1773451&r1=1773450&r2=1773451&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources.java
[UTF-8] Fri Dec  9 18:41:04 2016
@@ -61,6 +61,11 @@ public final class Resources extends Ind
         }
 
         /**
+         * TIFF file “{0}” has circular references in its chain of images.
+         */
+        public static final short CircularImageReference_1 = 18;
+
+        /**
          * No value specified for the “{0}” TIFF tag. Computed the {1} value from other
tags.
          */
         public static final short ComputedValueForAttribute_2 = 0;

Modified: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources.properties?rev=1773451&r1=1773450&r2=1773451&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources.properties
[ISO-8859-1] Fri Dec  9 18:41:04 2016
@@ -19,6 +19,7 @@
 # Resources in this file are for "sis-geotiff" usage only and should not be used by any other
module.
 # For resources shared by all modules in the Apache SIS project, see "org.apache.sis.util.resources"
package.
 #
+CircularImageReference_1          = TIFF file \u201c{0}\u201d has circular references in
its chain of images.
 ConstantValueRequired_3           = Apache SIS implementation requires that all \u201c{0}\u201d
elements have the same value, but the element found in \u201c{1}\u201d are {2}.
 ComputedValueForAttribute_2       = No value specified for the \u201c{0}\u201d TIFF tag.
Computed the {1} value from other tags.
 DefaultValueForAttribute_2        = No value specified for the \u201c{0}\u201d TIFF tag.
The {1} default value will be used.

Modified: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources_fr.properties?rev=1773451&r1=1773450&r2=1773451&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources_fr.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Resources_fr.properties
[ISO-8859-1] Fri Dec  9 18:41:04 2016
@@ -24,6 +24,7 @@
 #   U+202F NARROW NO-BREAK SPACE  before  ; ! and ?
 #   U+00A0 NO-BREAK SPACE         before  :
 #
+CircularImageReference_1          = Le fichier TIFF \u00ab\u202f{0}\u202f\u00bb a des r\u00e9f\u00e9rences
circulaires dans sa cha\u00eene d\u2019images.
 ConstantValueRequired_3           = L\u2019impl\u00e9mentation de Apache SIS requiert que
tous les \u00e9l\u00e9ments de \u00ab\u202f{0}\u202f\u00bb aient la m\u00eame valeur, mais
les \u00e9l\u00e9ments trouv\u00e9s dans \u00ab\u202f{1}\u202f\u00bb sont {2}.
 ComputedValueForAttribute_2       = Aucune valeur n\u2019a \u00e9t\u00e9 sp\u00e9cifi\u00e9e
pour le tag TIFF \u00ab\u202f{0}\u202f\u00bb. La valeur {1} a \u00e9t\u00e9 calcul\u00e9e
\u00e0 partir des autres tags.
 DefaultValueForAttribute_2        = Aucune valeur n\u2019a \u00e9t\u00e9 sp\u00e9cifi\u00e9e
pour le tag TIFF \u00ab\u202f{0}\u202f\u00bb. La valeur par d\u00e9faut sera {1}.

Modified: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Reader.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Reader.java?rev=1773451&r1=1773450&r2=1773451&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Reader.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Reader.java
[UTF-8] Fri Dec  9 18:41:04 2016
@@ -19,6 +19,8 @@ package org.apache.sis.storage.geotiff;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.LinkedList;
+import java.util.Set;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.io.IOException;
 import java.nio.ByteOrder;
@@ -27,6 +29,7 @@ import org.apache.sis.storage.DataStoreE
 import org.apache.sis.storage.DataStoreContentException;
 import org.apache.sis.internal.storage.ChannelDataInput;
 import org.apache.sis.internal.storage.MetadataBuilder;
+import org.apache.sis.internal.geotiff.Resources;
 import org.apache.sis.util.resources.Errors;
 
 
@@ -79,10 +82,21 @@ final class Reader extends GeoTIFF {
     /**
      * Offset (relative to the beginning of the TIFF file) of the next Image File Directory
(IFD)
      * to read, or 0 if we have finished to read all of them.
+     *
+     * @see #readNextImageOffset()
      */
     private long nextIFD;
 
     /**
+     * Offsets of all <cite>Image File Directory</cite> (IFD) that have been
read so far.
+     * This field is used only as a protection against infinite recursivity, by preventing
+     * the same offset to appear twice.
+     *
+     * @see #readNextImageOffset()
+     */
+    private final Set<Long> doneIFD;
+
+    /**
      * Positions of each <cite>Image File Directory</cite> (IFD) in this file.
      * Those positions are fetched when first needed.
      */
@@ -121,6 +135,7 @@ final class Reader extends GeoTIFF {
         this.input    = input;
         this.origin   = input.getStreamPosition();
         this.metadata = new MetadataBuilder();
+        this.doneIFD  = new HashSet<>();
         /*
          * A TIFF file begins with either "II" (0x4949) or "MM" (0x4D4D) characters.
          * Those characters identify the byte order. Note that we do not need to care
@@ -141,7 +156,7 @@ final class Reader extends GeoTIFF {
             switch (input.readShort()) {
                 case 42: {                                          // Magic number of classical
format.
                     intSizeExpansion = 0;
-                    nextIFD = input.readUnsignedInt();
+                    readNextImageOffset();
                     return;
                 }
                 case 43: {                                          // Magic number of BigTIFF
format.
@@ -155,7 +170,7 @@ final class Reader extends GeoTIFF {
                              * one result in the end, but we did that generic computation
anyway for keeping the
                              * code almost ready if the BigTIFF specification adds support
for 16 bytes pointer.
                              */
-                            nextIFD = readUnsignedInt();
+                            readNextImageOffset();
                             return;
                         }
                     }
@@ -167,6 +182,18 @@ final class Reader extends GeoTIFF {
     }
 
     /**
+     * Sets {@link #nextIFD} to the next offset read from the TIFF file
+     * and makes sure that it will not cause an infinite loop.
+     */
+    private void readNextImageOffset() throws IOException, DataStoreException {
+        nextIFD = readUnsignedInt();
+        if (!doneIFD.add(nextIFD)) {
+            throw new DataStoreContentException(resources().getString(
+                    Resources.Keys.CircularImageReference_1, input.filename));
+        }
+    }
+
+    /**
      * Returns a default message for parsing error.
      */
     final String canNotDecode() {
@@ -276,7 +303,7 @@ final class Reader extends GeoTIFF {
                 }
             }
             imageFileDirectories.add(dir);
-            nextIFD = readUnsignedInt();                    // Zero if the IFD that we just
read was the last one.
+            readNextImageOffset();                          // Zero if the IFD that we just
read was the last one.
         }
         /*
          * At this point we got the requested IFD. But maybe some deferred entries need to
be read.



Mime
View raw message