From aa98d5d20b33cfed2bf62db263fcb2cb8b36a81c Mon Sep 17 00:00:00 2001 From: Phil Race Date: Thu, 4 Jun 2026 19:09:19 +0000 Subject: [PATCH] 8385100: Null pointer dereference in java.desktop/windows/classes/sun/print/Win32PrintJob.java:606 and other PrintJob implementations Reviewed-by: psadhukhan, kizune --- .../classes/sun/print/PSStreamPrintJob.java | 4 +- .../unix/classes/sun/print/UnixPrintJob.java | 4 +- .../classes/sun/print/Win32PrintJob.java | 4 +- .../javax/print/NullUserNamePrintingTest.java | 58 ++++++++++++++ .../print/NullUserNameStreamPrintingTest.java | 75 +++++++++++++++++++ 5 files changed, 139 insertions(+), 6 deletions(-) create mode 100644 test/jdk/javax/print/NullUserNamePrintingTest.java create mode 100644 test/jdk/javax/print/NullUserNameStreamPrintingTest.java diff --git a/src/java.desktop/share/classes/sun/print/PSStreamPrintJob.java b/src/java.desktop/share/classes/sun/print/PSStreamPrintJob.java index dcafdb86460..0f420186f60 100644 --- a/src/java.desktop/share/classes/sun/print/PSStreamPrintJob.java +++ b/src/java.desktop/share/classes/sun/print/PSStreamPrintJob.java @@ -436,8 +436,8 @@ public class PSStreamPrintJob implements CancelablePrintJob { /* add the user name to the job */ String userName = System.getProperty("user.name"); if (userName == null || userName.isEmpty()) { - RequestingUserName ruName = - (RequestingUserName)reqSet.get(RequestingUserName.class); + RequestingUserName ruName = (reqSet != null) ? + (RequestingUserName)reqSet.get(RequestingUserName.class) : null; if (ruName != null) { jobAttrSet.add( new JobOriginatingUserName(ruName.getValue(), diff --git a/src/java.desktop/unix/classes/sun/print/UnixPrintJob.java b/src/java.desktop/unix/classes/sun/print/UnixPrintJob.java index 4604167a0e4..c75efac714a 100644 --- a/src/java.desktop/unix/classes/sun/print/UnixPrintJob.java +++ b/src/java.desktop/unix/classes/sun/print/UnixPrintJob.java @@ -716,8 +716,8 @@ public final class UnixPrintJob implements CancelablePrintJob { /* add the user name to the job */ String userName = System.getProperty("user.name"); if (userName == null || userName.isEmpty()) { - RequestingUserName ruName = - (RequestingUserName)reqSet.get(RequestingUserName.class); + RequestingUserName ruName = (reqSet != null) ? + (RequestingUserName)reqSet.get(RequestingUserName.class) : null; if (ruName != null) { jobAttrSet.add( new JobOriginatingUserName(ruName.getValue(), diff --git a/src/java.desktop/windows/classes/sun/print/Win32PrintJob.java b/src/java.desktop/windows/classes/sun/print/Win32PrintJob.java index 0afd0ed60c7..48cf1472016 100644 --- a/src/java.desktop/windows/classes/sun/print/Win32PrintJob.java +++ b/src/java.desktop/windows/classes/sun/print/Win32PrintJob.java @@ -602,8 +602,8 @@ public final class Win32PrintJob implements CancelablePrintJob { String userName = System.getProperty("user.name"); if (userName == null || userName.isEmpty()) { - RequestingUserName ruName = - (RequestingUserName)reqSet.get(RequestingUserName.class); + RequestingUserName ruName = (reqSet != null) ? + (RequestingUserName)reqSet.get(RequestingUserName.class) : null; if (ruName != null) { jobAttrSet.add( new JobOriginatingUserName(ruName.getValue(), diff --git a/test/jdk/javax/print/NullUserNamePrintingTest.java b/test/jdk/javax/print/NullUserNamePrintingTest.java new file mode 100644 index 00000000000..5051959447b --- /dev/null +++ b/test/jdk/javax/print/NullUserNamePrintingTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2026, 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 + * 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. + */ + +/* + * @test + * @bug 8385100 + * @key printer + * @summary Should not get NPE if atttribute set is null. + */ + +import java.io.ByteArrayInputStream; +import javax.print.Doc; +import javax.print.DocFlavor; +import javax.print.DocPrintJob; +import javax.print.PrintException; +import javax.print.PrintService; +import javax.print.PrintServiceLookup; +import javax.print.SimpleDoc; + +public class NullUserNamePrintingTest { + + public static void main(String[] args) throws PrintException { + PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null); + if (services.length == 0) { + System.err.println("This test requires a printer."); + return; + } + PrintService service = services[0]; + + DocPrintJob job = service.createPrintJob(); + + Doc doc = new SimpleDoc( + new ByteArrayInputStream("Test print".getBytes()), + DocFlavor.INPUT_STREAM.AUTOSENSE, null); + System.setProperty("user.name", ""); + job.print(doc, null); + } +} diff --git a/test/jdk/javax/print/NullUserNameStreamPrintingTest.java b/test/jdk/javax/print/NullUserNameStreamPrintingTest.java new file mode 100644 index 00000000000..475b8acbfd7 --- /dev/null +++ b/test/jdk/javax/print/NullUserNameStreamPrintingTest.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2026, 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 + * 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. + */ + +/* + * @test + * @bug 8385100 + * @summary Should not get NPE if atttribute set is null. + */ + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.awt.Graphics; +import java.awt.print.PageFormat; +import java.awt.print.Printable; +import java.awt.print.PrinterException; +import javax.print.Doc; +import javax.print.DocFlavor; +import javax.print.DocPrintJob; +import javax.print.PrintException; +import javax.print.PrintService; +import javax.print.PrintServiceLookup; +import javax.print.SimpleDoc; +import javax.print.StreamPrintService; +import javax.print.StreamPrintServiceFactory; + +public class NullUserNameStreamPrintingTest implements Printable { + + public static void main(String[] args) throws PrintException { + + DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; + String mime = "application/postscript"; + StreamPrintServiceFactory[] factories = + StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, mime); + if (factories.length == 0) { + throw new RuntimeException("Unable to find PostScript print service factory"); + } + StreamPrintServiceFactory factory = factories[0]; + ByteArrayOutputStream output = new ByteArrayOutputStream(); + StreamPrintService service = factory.getPrintService(output); + + DocPrintJob job = service.createPrintJob(); + + Doc doc = new SimpleDoc(new NullUserNameStreamPrintingTest(), flavor, null); + System.setProperty("user.name", ""); + job.print(doc, null); + } + + public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) + throws PrinterException { + if (pageIndex > 0) { + return NO_SUCH_PAGE; + } + return PAGE_EXISTS; + } +}