From aca0952d604e00d0332ea48a8dc4c59aca614154 Mon Sep 17 00:00:00 2001 From: Gennadiy Krivoshein Date: Thu, 23 Jan 2025 13:34:54 +0000 Subject: [PATCH] Fix ArrayIndexOutOfBoundsException at PSPrinterJob printExecCmd --- .../share/classes/sun/print/PSPrinterJob.java | 12 +-- .../javax/print/PrintExecCmdOptionTest.java | 100 ++++++++++++++++++ 2 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 test/jdk/javax/print/PrintExecCmdOptionTest.java diff --git a/src/java.desktop/share/classes/sun/print/PSPrinterJob.java b/src/java.desktop/share/classes/sun/print/PSPrinterJob.java index eb981c92a12..f7aef439428 100644 --- a/src/java.desktop/share/classes/sun/print/PSPrinterJob.java +++ b/src/java.desktop/share/classes/sun/print/PSPrinterJob.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -1614,9 +1614,8 @@ private String[] printExecCmd(String printer, String options, execCmd[n++] = "-o job-sheets=standard"; } if ((pFlags & OPTIONS) != 0) { - for (String option : options.trim().split(" ")) { - execCmd[n++] = "-o " + option; - } + execCmd[n++] = "-o " + options.trim(). + replaceAll("\\s+", " -o "); } } else { ncomps+=1; //add 1 arg for lp @@ -1639,9 +1638,8 @@ private String[] printExecCmd(String printer, String options, execCmd[n++] = "-o job-sheets=standard"; } if ((pFlags & OPTIONS) != 0) { - for (String option : options.trim().split(" ")) { - execCmd[n++] = "-o " + option; - } + execCmd[n++] = "-o " + options.trim(). + replaceAll("\\s+", " -o "); } } execCmd[n++] = spoolFile; diff --git a/test/jdk/javax/print/PrintExecCmdOptionTest.java b/test/jdk/javax/print/PrintExecCmdOptionTest.java new file mode 100644 index 00000000000..9862063cb30 --- /dev/null +++ b/test/jdk/javax/print/PrintExecCmdOptionTest.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2025, BELLSOFT. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import javax.print.PrintService; +import javax.print.PrintServiceLookup; +import javax.print.attribute.HashPrintRequestAttributeSet; +import javax.print.attribute.PrintRequestAttributeSet; +import javax.print.attribute.standard.Media; +import javax.print.attribute.standard.MediaTray; +import javax.print.attribute.standard.OutputBin; +import java.awt.Graphics; +import java.awt.print.PageFormat; +import java.awt.print.Printable; +import java.awt.print.PrinterException; +import java.awt.print.PrinterJob; + +/* + * @test + * @bug 0000000 + * @key printer + * @summary no ArrayIndexOutOfBoundsException with multiple print options + * @run main PrintExecCmdOptionTest + */ + +public class PrintExecCmdOptionTest { + + public static void main(String[] args) throws PrinterException { + PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); + if (printServices.length == 0) { + System.out.println("No print service found"); + return; + } + + PrintService testPrintService = null; + OutputBin outputBin = null; + MediaTray mediaTray = null; + for (PrintService ps : printServices) { + Media[] medias = (Media[]) ps. + getSupportedAttributeValues(Media.class, null, null); + if (medias != null) { + for (Media m : medias) { + if (m instanceof MediaTray) { + mediaTray = (MediaTray) m; + break; + } + } + } + if (mediaTray == null) { + continue; + } + OutputBin[] outputBins = (OutputBin[]) ps. + getSupportedAttributeValues(OutputBin.class, null, null); + if (outputBins != null && outputBins.length > 0) { + outputBin = outputBins[0]; + testPrintService = ps; + break; + } + } + if (testPrintService == null) { + System.out.println("Test print service not found"); + return; + } + + PrinterJob printerJob = PrinterJob.getPrinterJob(); + printerJob.setPrintService(testPrintService); + + PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet(); + attributeSet.add(outputBin); + attributeSet.add(mediaTray); + + printerJob.setPrintable(new Printable() { + @Override + public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) { + return NO_SUCH_PAGE; + } + }); + printerJob.print(attributeSet); + } +} -- 2.34.1