From daee81d4a1a4b17c08ed28c68002e44b08754aaf Mon Sep 17 00:00:00 2001 From: stepan <stepan.sindelar@oracle.com> Date: Mon, 2 Oct 2017 14:33:34 +0200 Subject: [PATCH] Do not leak NPE from BufferedImageDevice when the IO is not successful --- .../fastrGrid/device/awt/BufferedImageDevice.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/device/awt/BufferedImageDevice.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/device/awt/BufferedImageDevice.java index 1f95c9cfd1..c392fed266 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/device/awt/BufferedImageDevice.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/device/awt/BufferedImageDevice.java @@ -28,7 +28,10 @@ import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import javax.imageio.ImageIO; @@ -72,9 +75,18 @@ public final class BufferedImageDevice extends Graphics2DDevice implements FileG private void saveImage() throws DeviceCloseException { try { + if (!Files.exists(Paths.get(filename).getParent())) { + // Bug in JDK? when the path contains directory that does not exist, the code throws + // NPE and prints out to the standard output (!) stack trace of + // FileNotFoundException. We still catch the exception, because this check and + // following Image.write are not atomic. + throw new DeviceCloseException(new FileNotFoundException("Path " + filename + " does not exist")); + } ImageIO.write(image, fileType, new File(filename)); } catch (IOException e) { throw new DeviceCloseException(e); + } catch (NullPointerException npe) { + throw new DeviceCloseException(new FileNotFoundException("Path " + filename + " does not exist")); } } -- GitLab