8169725: cannot use TIFFField(TIFFTag tag, int value) for TIFF_LONG values greater than Integer.MAX_VALUE
authorbpb
Fri, 09 Dec 2016 11:08:38 -0800
changeset 42742 60484cf11911
parent 42741 3254386c787e
child 42743 e61f0c011e93
8169725: cannot use TIFFField(TIFFTag tag, int value) for TIFF_LONG values greater than Integer.MAX_VALUE Summary: Change constructor TIFFField(TIFFTag,int) to TIFFField(TIFFTag,long). Reviewed-by: jdv, prr
jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageMetadata.java
jdk/src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java
jdk/test/javax/imageio/plugins/tiff/TIFFFieldTest.java
--- a/jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageMetadata.java	Fri Dec 09 12:42:17 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageMetadata.java	Fri Dec 09 11:08:38 2016 -0800
@@ -87,7 +87,7 @@
         rootIFD.initialize(stream, true, ignoreUnknownFields);
     }
 
-    public void addShortOrLongField(int tagNumber, int value) {
+    public void addShortOrLongField(int tagNumber, long value) {
         TIFFField field = new TIFFField(rootIFD.getTag(tagNumber), value);
         rootIFD.addTIFFField(field);
     }
--- a/jdk/src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java	Fri Dec 09 12:42:17 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java	Fri Dec 09 11:08:38 2016 -0800
@@ -658,27 +658,32 @@
 
     /**
      * Constructs a {@code TIFFField} with a single non-negative integral
-     * value.
-     * The field will have type
-     * {@link TIFFTag#TIFF_SHORT  TIFF_SHORT} if
-     * {@code val < 65536} and type
-     * {@link TIFFTag#TIFF_LONG TIFF_LONG} otherwise.  The count
-     * of the field will be unity.
+     * value. The field will have type {@link TIFFTag#TIFF_SHORT TIFF_SHORT}
+     * if {@code value} is in {@code [0,0xffff]}, and type
+     * {@link TIFFTag#TIFF_LONG TIFF_LONG} if {@code value} is in
+     * {@code [0x10000,0xffffffff]}. The count of the field will be unity.
      *
      * @param tag The tag to associate with this field.
      * @param value The value to associate with this field.
      * @throws NullPointerException if {@code tag == null}.
-     * @throws IllegalArgumentException if the derived type is unacceptable
-     * for the supplied {@code TIFFTag}.
-     * @throws IllegalArgumentException if {@code value < 0}.
+     * @throws IllegalArgumentException if {@code value} is not in
+     * {@code [0,0xffffffff]}.
+     * @throws IllegalArgumentException if {@code value} is in
+     * {@code [0,0xffff]} and {@code TIFF_SHORT} is an unacceptable type
+     * for the {@code TIFFTag}, or if {@code value} is in
+     * {@code [0x10000,0xffffffff]} and {@code TIFF_LONG} is an unacceptable
+     * type for the {@code TIFFTag}.
      */
-    public TIFFField(TIFFTag tag, int value) {
+    public TIFFField(TIFFTag tag, long value) {
         if(tag == null) {
             throw new NullPointerException("tag == null!");
         }
         if (value < 0) {
             throw new IllegalArgumentException("value < 0!");
         }
+        if (value > 0xffffffffL) {
+            throw new IllegalArgumentException("value > 0xffffffff!");
+        }
 
         this.tag = tag;
         this.tagNumber = tag.getNumber();
@@ -687,7 +692,8 @@
         if (value < 65536) {
             if (!tag.isDataTypeOK(TIFFTag.TIFF_SHORT)) {
                 throw new IllegalArgumentException("Illegal data type "
-                    + TIFFTag.TIFF_SHORT + " for " + tag.getName() + " tag");
+                    + getTypeName(TIFFTag.TIFF_SHORT) + " for tag "
+                    + "\"" + tag.getName() + "\"");
             }
             this.type = TIFFTag.TIFF_SHORT;
             char[] cdata = new char[1];
@@ -696,7 +702,8 @@
         } else {
             if (!tag.isDataTypeOK(TIFFTag.TIFF_LONG)) {
                 throw new IllegalArgumentException("Illegal data type "
-                    + TIFFTag.TIFF_LONG + " for " + tag.getName() + " tag");
+                    + getTypeName(TIFFTag.TIFF_LONG) + " for tag "
+                    + "\"" + tag.getName() + "\"");
             }
             this.type = TIFFTag.TIFF_LONG;
             long[] ldata = new long[1];
--- a/jdk/test/javax/imageio/plugins/tiff/TIFFFieldTest.java	Fri Dec 09 12:42:17 2016 +0300
+++ b/jdk/test/javax/imageio/plugins/tiff/TIFFFieldTest.java	Fri Dec 09 11:08:38 2016 -0800
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug     8152183 8149562
+ * @bug     8152183 8149562 8169725
  * @author  a.stepanov
  * @summary Some checks for TIFFField methods
  * @run     main TIFFFieldTest
@@ -65,7 +65,26 @@
         ok = false;
         try { new TIFFField(tag, -1); }
         catch (IllegalArgumentException e) { ok = true; }
-        check(ok, CONSTRUCT + "invalid count");
+        check(ok, CONSTRUCT + "negative value");
+
+        ok = false;
+        try { new TIFFField(tag, 1L << 32); }
+        catch (IllegalArgumentException e) { ok = true; }
+        check(ok, CONSTRUCT + "value > 0xffffffff");
+
+        ok = false;
+        try {
+            TIFFTag t = new TIFFTag(NAME, NUM, 1 << TIFFTag.TIFF_SHORT);
+            new TIFFField(t, 0x10000);
+        } catch (IllegalArgumentException e) { ok = true; }
+        check(ok, CONSTRUCT + "value 0x10000 incompatible with TIFF_SHORT");
+
+        ok = false;
+        try {
+            TIFFTag t = new TIFFTag(NAME, NUM, 1 << TIFFTag.TIFF_LONG);
+            new TIFFField(t, 0xffff);
+        } catch (IllegalArgumentException e) { ok = true; }
+        check(ok, CONSTRUCT + "value 0xffff incompatible with TIFF_LONG");
 
         // check value type recognition
         int v = 1 << 16;