--- old/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java 2017-11-13 14:48:12.270406000 +0530 +++ new/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java 2017-11-13 14:48:11.738406000 +0530 @@ -1359,14 +1359,13 @@ this.pixelStream = new DataInputStream(is); /* - * NB: the PNG spec declares that valid range for width + * PNG spec declares that valid range for width * and height is [1, 2^31-1], so here we may fail to allocate * a buffer for destination image due to memory limitation. * - * However, the recovery strategy for this case should be - * defined on the level of application, so we will not - * try to estimate the required amount of the memory and/or - * handle OOM in any way. + * So if we get any OOM error at this stage we will wrap the + * OOM error into an IIOException at higher level like + * PNGImageReader.read(int, ImageReadParam). */ theImage = getDestination(param, getImageTypes(0), @@ -1671,7 +1670,18 @@ throw new IndexOutOfBoundsException("imageIndex != 0!"); } - readImage(param); + try { + readImage(param); + } catch (IOException | + IndexOutOfBoundsException | + IllegalStateException | + IllegalArgumentException e) + { + throw e; + } catch (Throwable e) { + throw new IIOException("BufferOverflow/OOM while reading" + + " the image"); + } return theImage; }