changeset 55137:03924ad3f4d0

8219548: Better Null paramenter checking in ToolProvider Reviewed-by: lancea, alanb, jjg Contributed-by: Philipp Kunz <philipp.kunz@paratix.ch>
author lancea
date Tue, 26 Feb 2019 13:14:26 -0500
parents 7272e7c5164b
children 20ee41e8740d
files src/java.base/share/classes/java/util/spi/ToolProvider.java test/jdk/java/util/spi/ToolProviderTest.java
diffstat 2 files changed, 35 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/util/spi/ToolProvider.java	Tue Feb 19 16:13:05 2019 +0000
+++ b/src/java.base/share/classes/java/util/spi/ToolProvider.java	Tue Feb 26 13:14:26 2019 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2019, 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
@@ -126,8 +126,9 @@
     default int run(PrintStream out, PrintStream err, String... args) {
         Objects.requireNonNull(out);
         Objects.requireNonNull(err);
+        Objects.requireNonNull(args);
         for (String arg : args) {
-            Objects.requireNonNull(args);
+            Objects.requireNonNull(arg);
         }
 
         PrintWriter outWriter = new PrintWriter(out);
--- a/test/jdk/java/util/spi/ToolProviderTest.java	Tue Feb 19 16:13:05 2019 +0000
+++ b/test/jdk/java/util/spi/ToolProviderTest.java	Tue Feb 26 13:14:26 2019 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2019, 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
@@ -28,7 +28,6 @@
  * @run main/othervm ToolProviderTest
  */
 
-import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.nio.file.Files;
@@ -46,6 +45,10 @@
     void run() throws Exception {
         initServices();
 
+        System.out.println("Validate an NPE is thrown with null arguments");
+
+        testNullArgs();
+
         System.out.println("test without security manager present:");
         test();
 
@@ -63,6 +66,33 @@
         }
     }
 
+    private void testNullArgs() {
+        ToolProvider testProvider = ToolProvider.findFirst("test").get();
+
+        // out null check
+        expectNullPointerException(() -> testProvider.run(null, System.err));
+
+        // err null check
+        expectNullPointerException(() -> testProvider.run(System.out, null));
+
+        // args array null check
+        expectNullPointerException(() ->
+                testProvider.run(System.out, System.err, (String[]) null));
+
+        // args array elements null check
+        expectNullPointerException(() ->
+                testProvider.run(System.out, System.err, (String) null));
+    }
+
+    private static void expectNullPointerException(Runnable test) {
+        try {
+            test.run();
+            throw new Error("NullPointerException not thrown");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+
     private void initServices() throws IOException {
         Path testClasses = Paths.get(System.getProperty("test.classes"));
         Path services = testClasses.resolve(Paths.get("META-INF", "services"));