8180447: Trailing space in JDK_JAVA_OPTIONS causes an application fail to launch
authorhenryjen
Wed, 17 May 2017 16:26:23 -0700
changeset 45252 654115968257
parent 45251 f7d47a5e1e99
child 45253 7d08b205b036
8180447: Trailing space in JDK_JAVA_OPTIONS causes an application fail to launch Reviewed-by: alanb, mchung, ksrini
jdk/src/java.base/share/native/libjli/args.c
jdk/src/java.base/share/native/libjli/emessages.h
jdk/test/tools/launcher/ArgsEnvVar.java
--- a/jdk/src/java.base/share/native/libjli/args.c	Wed May 17 14:29:26 2017 -0700
+++ b/jdk/src/java.base/share/native/libjli/args.c	Wed May 17 16:26:23 2017 -0700
@@ -456,6 +456,11 @@
             env++;
         }
 
+        // Trailing space
+        if (*env == '\0') {
+            break;
+        }
+
         arg = p;
         while (*env != '\0' && !isspace(*env)) {
             if (*env == '"' || *env == '\'') {
--- a/jdk/src/java.base/share/native/libjli/emessages.h	Wed May 17 14:29:26 2017 -0700
+++ b/jdk/src/java.base/share/native/libjli/emessages.h	Wed May 17 16:26:23 2017 -0700
@@ -36,7 +36,7 @@
 #define JNI_ERROR       "Error: A JNI error has occurred, please check your installation and try again"
 #define JNI_ERROR1      "Error: can't find JNI interfaces in: %s"
 
-#define ARG_INFO_ENVVAR "NOTE: Picked up the following options via %s:\n  %s"
+#define ARG_INFO_ENVVAR "NOTE: Picked up %s: %s"
 #define ARG_WARN        "Warning: %s option is no longer supported."
 
 #define ARG_ERROR1      "Error: %s requires class path specification"
--- a/jdk/test/tools/launcher/ArgsEnvVar.java	Wed May 17 14:29:26 2017 -0700
+++ b/jdk/test/tools/launcher/ArgsEnvVar.java	Wed May 17 16:26:23 2017 -0700
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 8170832
+ * @bug 8170832 8180447
  * @summary Arguments passed in environment variable
  * @build TestHelper
  * @run main ArgsEnvVar
@@ -224,6 +224,21 @@
         verifyOptions(List.of("-p", "?", "-jar", "test.jar", "one", "two"), tr);
     }
 
+    @Test
+    public void testTrailingSpaces() {
+        env.put(JDK_JAVA_OPTIONS, "--add-exports java.base/jdk.internal.misc=ALL-UNNAMED ");
+        TestResult tr = doExec(env, javaCmd, "-jar", "test.jar");
+        verifyOptions(List.of("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", "-jar", "test.jar"), tr);
+
+        env.put(JDK_JAVA_OPTIONS, "--class-path ' '");
+        tr = doExec(env, javaCmd, "-jar", "test.jar");
+        verifyOptions(List.of("--class-path", " ", "-jar", "test.jar"), tr);
+
+        env.put(JDK_JAVA_OPTIONS, "  --add-exports java.base/jdk.internal.misc=ALL-UNNAMED ");
+        tr = doExec(env, javaCmd, "-jar", "test.jar");
+        verifyOptions(List.of("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", "-jar", "test.jar"), tr);
+    }
+
     public static void main(String... args) throws Exception {
         init();
         ArgsEnvVar a = new ArgsEnvVar();
@@ -236,4 +251,3 @@
         }
     }
 }
-