Path: blob/master/test/jdk/javax/imageio/plugins/png/PngLargeIHDRDimensionTest.java
41155 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 819033226* @summary Test verifies whether PNGImageReader throws IIOException27* or not when IHDR width value is very high.28* @run main PngLargeIHDRDimensionTest29*/3031import java.io.ByteArrayInputStream;32import java.io.InputStream;33import java.util.Base64;34import javax.imageio.IIOException;35import javax.imageio.ImageIO;3637public class PngLargeIHDRDimensionTest {3839/*40* IHDR width is very large and when we try to create buffer to store41* image information of each row it overflows and we get42* NegativeArraySizeException without the fix for this bug.43*/44private static String negativeArraySizeExceptionInput = "iVBORw0KGgoAAAANS"45+ "UhEUg////0AAAABEAIAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAAB"46+ "JRU5ErkJgggo=";4748/*49* IHDR width is ((2 to the power of 31) - 2), which is the maximum VM50* limit to create an array we get OutOfMemoryError without the fix51* for this bug.52*/53private static String outOfMemoryErrorInput = "iVBORw0KGgoAAAANSUhEUgAAAAF/"54+ "///+CAAAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAABJRU5"55+ "ErkJgggo=";5657private static InputStream input;58private static Boolean firstTestFailed = true, secondTestFailed = true;59public static void main(String[] args) throws java.io.IOException {60byte[] inputBytes = Base64.getDecoder().61decode(negativeArraySizeExceptionInput);62input = new ByteArrayInputStream(inputBytes);6364try {65ImageIO.read(input);66} catch (IIOException e) {67firstTestFailed = false;68}6970inputBytes = Base64.getDecoder().decode(outOfMemoryErrorInput);71input = new ByteArrayInputStream(inputBytes);7273try {74ImageIO.read(input);75} catch (IIOException e) {76secondTestFailed = false;77}7879if (firstTestFailed || secondTestFailed) {80throw new RuntimeException("Test doesn't throw required"81+ " IIOException");82}83}84}85868788