--- old/src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java 2017-01-11 18:03:50.882044571 +0530 +++ new/src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java 2017-01-11 18:03:50.674044571 +0530 @@ -698,6 +698,11 @@ * data type for the supplied {@code TIFFTag}. * @throws IllegalArgumentException if {@code count < 0}. * @see #TIFFField(TIFFTag,int,int,Object) + * @throws IllegalArgumentException if {@code count < 1} + * and {@code type} is {@code TIFF_RATIONAL} or + * {@code TIFF_SRATIONAL}. + * @throws IllegalArgumentException if {@code count != 1} + * and {@code type} is {@code TIFF_IFD_POINTER}. */ public TIFFField(TIFFTag tag, int type, int count) { this(tag, type, count, createArrayForType(type, count)); @@ -885,11 +890,26 @@ * @throws IllegalArgumentException if {@code dataType} is not * one of the {@code TIFFTag.TIFF_*} data type constants. * @throws IllegalArgumentException if {@code count < 0}. + * @throws IllegalArgumentException if {@code count < 1} + * and {@code type} is {@code TIFF_RATIONAL} or + * {@code TIFF_SRATIONAL}. + * @throws IllegalArgumentException if {@code count != 1} + * and {@code type} is {@code TIFF_IFD_POINTER}. */ public static Object createArrayForType(int dataType, int count) { + if(count < 0) { throw new IllegalArgumentException("count < 0!"); + } else if((dataType == TIFFTag.TIFF_RATIONAL + || dataType == TIFFTag.TIFF_SRATIONAL) + && count < 1) { + throw new IllegalArgumentException + ("Type is TIFF_RATIONAL or TIFF_SRATIONAL and count < 1"); + } else if (dataType == TIFFTag.TIFF_IFD_POINTER && count != 1) { + throw new IllegalArgumentException + ("Type is TIFF_IFD_POINTER count != 1"); } + switch (dataType) { case TIFFTag.TIFF_BYTE: case TIFFTag.TIFF_SBYTE: --- /dev/null 2017-01-11 11:24:22.974040000 +0530 +++ new/test/javax/imageio/plugins/tiff/TIFFCreateArrayForTypeTest.java 2017-01-11 18:03:51.274044571 +0530 @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @bug 8152561 + * @summary Test verifies whether all boundary conditions are checked + * properly in TIFFField.createArrayForType(). + * @run main TIFFCreateArrayForTypeTest + */ + +import javax.imageio.plugins.tiff.TIFFField; +import javax.imageio.plugins.tiff.TIFFTag; + +public class TIFFCreateArrayForTypeTest { + + static int count = 0; + + private static void check(boolean pass, String msg) { + if (!pass) { + throw new RuntimeException(msg); + } + } + public static void main(String[] args) { + + // check passing unknown data type to createArrayForType() + boolean pass = false; + count = 2; + int dataType = 15; + try { + TIFFField.createArrayForType(dataType, count); + } catch (IllegalArgumentException e) { + pass = true; + } catch (Exception e) { + // just consume if it throws any other exception. + } + check(pass, "Passing unknown datatype is not throwing" + + " IllegalArgumentException"); + + // check passing negative count value for createArrayForType() + pass = false; + count = -1; + try { + TIFFField.createArrayForType(TIFFTag.TIFF_LONG, count); + } catch (IllegalArgumentException e) { + pass = true; + } catch (Exception e) { + // just consume if it throws any other exception. + } + check(pass, "Passing negative count value is not throwing" + + " IllegalArgumentException"); + + /* + * check passing zero count value for createArrayForType() with + * TIFFTag.TIFF_RATIONAL or TIFFTag.TIFF_SRATIONAL data type. + */ + pass = false; + count = 0; + try { + TIFFField.createArrayForType(TIFFTag.TIFF_RATIONAL, count); + } catch (IllegalArgumentException e) { + pass = true; + } catch (Exception e) { + // just consume if it throws any other exception. + } + check(pass, "Passing zero count value with TIFFTag.TIFF_RATIONAL" + + " is not throwing IllegalArgumentException"); + + /* + * check passing count value other than 1 for createArrayForType() with + * TIFFTag.TIFF_IFD_POINTER data type. + */ + pass = false; + count = 2; + try { + TIFFField.createArrayForType(TIFFTag.TIFF_IFD_POINTER, count); + } catch (IllegalArgumentException e) { + pass = true; + } catch (Exception e) { + // just consume if it throws any other exception. + } + check(pass, "Passing count value other than 1 with" + + " TIFFTag.TIFF_IFD_POINTER is not throwing" + + " IllegalArgumentException"); + } +} +