From 167ab74d942b4e20c2712921a711b3d5a8f3aeb4 Mon Sep 17 00:00:00 2001 From: Simon Pepping Date: Mon, 9 Oct 2006 10:34:32 +0000 Subject: [PATCH] Prevent a StringIndexOutOfBoundsException when the PNG output file name does not have an extension. Patch submitted by Matt Healy , see bug 40655. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@454331 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/render/bitmap/PNGRenderer.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/java/org/apache/fop/render/bitmap/PNGRenderer.java b/src/java/org/apache/fop/render/bitmap/PNGRenderer.java index 42d7c2840..82eb65023 100644 --- a/src/java/org/apache/fop/render/bitmap/PNGRenderer.java +++ b/src/java/org/apache/fop/render/bitmap/PNGRenderer.java @@ -48,6 +48,9 @@ public class PNGRenderer extends Java2DRenderer { /** The MIME type for png-Rendering */ public static final String MIME_TYPE = MimeConstants.MIME_PNG; + /** The file extension expected for PNG files */ + private static final String PNG_FILE_EXTENSION = "png"; + /** The file syntax prefix, eg. "page" will output "page1.png" etc */ private String filePrefix; @@ -71,10 +74,15 @@ public class PNGRenderer extends Java2DRenderer { /** * Sets the output directory, either from the outfile specified on the - * command line, or from the directory specified in configuration file. Also - * sets the file name syntax, eg. "page" + * command line, or from the directory specified in configuration file. + * Also sets the file name syntax, eg. "page". + * The file name must not have an extension, or must have extension "png", + * and its last period must not be at the start (empty file prefix). + * + * @throws IOException if an invalid output file name was specified + * (e.g., with an extension other than '.png') */ - private void setOutputDirectory() { + private void setOutputDirectory() throws IOException { // the file provided on the command line File f = getUserAgent().getOutputFile(); @@ -88,12 +96,24 @@ public class PNGRenderer extends Java2DRenderer { // extracting file name syntax String s = f.getName(); int i = s.lastIndexOf("."); + if (i > 0) { + // Make sure that the file extension was "png" + String extension = s.substring(i + 1).toLowerCase(); + if (!PNG_FILE_EXTENSION.equals(extension)) { + throw new IOException("Invalid file extension ('" + + extension + "') specified"); + } + } else if (i == -1) { + i = s.length(); + } else { // i == 0 + throw new IOException("Invalid file name ('" + + s + "') specified"); + } if (s.charAt(i - 1) == '1') { i--; // getting rid of the "1" } filePrefix = s.substring(0, i); } - } /** @see org.apache.fop.render.Renderer#stopRenderer() */ @@ -147,7 +167,7 @@ public class PNGRenderer extends Java2DRenderer { return null; } else { File f = new File(outputDir, - filePrefix + (pageNumber + 1) + ".png"); + filePrefix + (pageNumber + 1) + "." + PNG_FILE_EXTENSION); try { OutputStream os = new BufferedOutputStream(new FileOutputStream(f)); return os; -- 2.39.5