changeset 16327:eca374b7abf3

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
author bpb
date Fri, 09 Dec 2016 11:08:38 -0800
parents d8efcad28072
children dc658d7dde90
files src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageMetadata.java src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java test/javax/imageio/plugins/tiff/TIFFFieldTest.java
diffstat 3 files changed, 41 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageMetadata.java	Fri Dec 09 12:42:17 2016 +0300
+++ b/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/src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java	Fri Dec 09 12:42:17 2016 +0300
+++ b/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/test/javax/imageio/plugins/tiff/TIFFFieldTest.java	Fri Dec 09 12:42:17 2016 +0300
+++ b/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;