1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     8190332
  27  * @summary Test verifies whether PNGImageReader throws IIOException
  28  *          or not when IHDR width value is very high.
  29  * @run     main PngLargeIHDRDimensionTest
  30  */
  31 
  32 import java.io.ByteArrayInputStream;
  33 import java.io.InputStream;
  34 import java.util.Base64;
  35 import javax.imageio.IIOException;
  36 import javax.imageio.ImageIO;
  37 
  38 public class PngLargeIHDRDimensionTest {
  39 
  40     /*
  41      * IHDR width is very large and when we try to create buffer to store
  42      * image information of each row it overflows and we get
  43      * NegativeArraySizeException without the fix for this bug.
  44      */
  45     private static String negativeArraySizeExceptionInput = "iVBORw0KGgoAAAANS"
  46             + "UhEUg////0AAAABEAIAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAAB"
  47             + "JRU5ErkJgggo=";
  48 
  49     /*
  50      * IHDR width is ((2 to the power of 31) - 2), which is the maximum VM
  51      * limit to create an array we get OutOfMemoryError without the fix
  52      * for this bug.
  53      */
  54     private static String outOfMemoryErrorInput = "iVBORw0KGgoAAAANSUhEUgAAAAF/"
  55             + "///+CAAAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAABJRU5"
  56             + "ErkJgggo=";
  57 
  58     private static InputStream input;
  59     private static Boolean firstTestFailed = true, secondTestFailed = true;
  60     public static void main(String[] args) throws java.io.IOException {
  61         byte[] inputBytes = Base64.getDecoder().
  62                 decode(negativeArraySizeExceptionInput);
  63         input = new ByteArrayInputStream(inputBytes);
  64 
  65         try {
  66             ImageIO.read(input);
  67         } catch (IIOException e) {
  68             firstTestFailed = false;
  69         }
  70 
  71         inputBytes = Base64.getDecoder().decode(outOfMemoryErrorInput);
  72         input = new ByteArrayInputStream(inputBytes);
  73 
  74         try {
  75             ImageIO.read(input);
  76         } catch (IIOException e) {
  77             secondTestFailed = false;
  78         }
  79 
  80         if (firstTestFailed || secondTestFailed) {
  81             throw new RuntimeException("Test doesn't throw required"
  82                     + " IIOException");
  83         }
  84     }
  85 }
  86