# HG changeset patch # User bpb # Date 1481160727 28800 # Wed Dec 07 17:32:07 2016 -0800 # Node ID 605fb4ea4703d21b6fab2647c7bd4e52571c3e0a # Parent 2fe8bd250ebc0e1c88d9141ccb694066f2c75baf 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 diff --git a/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageMetadata.java b/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageMetadata.java --- a/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageMetadata.java +++ b/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageMetadata.java @@ -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); } diff --git a/src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java b/src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java --- a/src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java +++ b/src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java @@ -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]; diff --git a/test/javax/imageio/plugins/tiff/TIFFFieldTest.java b/test/javax/imageio/plugins/tiff/TIFFFieldTest.java --- a/test/javax/imageio/plugins/tiff/TIFFFieldTest.java +++ b/test/javax/imageio/plugins/tiff/TIFFFieldTest.java @@ -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;