8231863: Crash if classpath is read from @argument file and the main gets option argument
authorhenryjen
Mon, 11 Nov 2019 17:43:10 -0800
changeset 59020 aebd72de84b0
parent 59017 51196a273f8c
child 59021 cfc7bb9a5a92
8231863: Crash if classpath is read from @argument file and the main gets option argument Reviewed-by: alanb, mchung Contributed-by: Mat Carter <matthew.carter@microsoft.com>
src/java.base/share/native/libjli/args.c
test/jdk/tools/launcher/ArgsFileTest.java
test/jdk/tools/launcher/TestHelper.java
--- a/src/java.base/share/native/libjli/args.c	Mon Nov 11 11:09:49 2019 -0800
+++ b/src/java.base/share/native/libjli/args.c	Mon Nov 11 17:43:10 2019 -0800
@@ -337,7 +337,9 @@
     // remaining partial token
     if (ctx.state == IN_TOKEN || ctx.state == IN_QUOTE) {
         if (ctx.parts->size != 0) {
-            JLI_List_add(rv, JLI_List_combine(ctx.parts));
+            token = JLI_List_combine(ctx.parts);
+            checkArg(token);
+            JLI_List_add(rv, token);
         }
     }
     JLI_List_free(ctx.parts);
--- a/test/jdk/tools/launcher/ArgsFileTest.java	Mon Nov 11 11:09:49 2019 -0800
+++ b/test/jdk/tools/launcher/ArgsFileTest.java	Mon Nov 11 17:43:10 2019 -0800
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 8027634
+ * @bug 8027634 8231863
  * @summary Argument parsing from file
  * @modules jdk.compiler
  *          jdk.zipfs
@@ -61,13 +61,17 @@
         env.put(JLDEBUG_KEY, "true");
     }
 
-    private File createArgFile(String fname, List<String> lines) throws IOException {
+    private File createArgFile(String fname, List<String> lines, boolean endWithNewline) throws IOException {
         File argFile = new File(fname);
         argFile.delete();
-        createAFile(argFile, lines);
+        createAFile(argFile, lines, endWithNewline);
         return argFile;
     }
 
+    private File createArgFile(String fname, List<String> lines) throws IOException {
+        return createArgFile(fname, lines, true);
+    }
+
     private void verifyOptions(List<String> args, TestResult tr) {
         if (args.isEmpty()) {
             return;
@@ -266,6 +270,23 @@
         userArgs.delete();
     }
 
+    @Test
+    public void userApplicationWithoutEmptyLastLine() throws IOException {
+        File cpOpt = createArgFile("cpOpt", Arrays.asList("-classpath ."), false);
+        File vmArgs = createArgFile("vmArgs", Arrays.asList("-Xint"), false);
+
+        TestResult tr = doExec(env, javaCmd, "-cp", "test.jar", "@cpOpt", "Foo", "-test");
+        verifyOptions(Arrays.asList("-cp", "test.jar", "-classpath", ".", "Foo", "-test"), tr);
+        verifyUserArgs(Arrays.asList("-test"), tr, 6);
+
+        tr = doExec(env, javaCmd,  "-cp", "test.jar", "@vmArgs", "Foo", "-test");
+        verifyOptions(Arrays.asList("-cp", "test.jar", "-Xint", "Foo", "-test"), tr);
+        verifyUserArgs(Arrays.asList("-test"), tr, 5);
+
+        cpOpt.delete();
+        vmArgs.delete();
+    }
+
     // test with missing file
     @Test
     public void missingFileNegativeTest() throws IOException {
--- a/test/jdk/tools/launcher/TestHelper.java	Mon Nov 11 11:09:49 2019 -0800
+++ b/test/jdk/tools/launcher/TestHelper.java	Mon Nov 11 17:43:10 2019 -0800
@@ -349,12 +349,23 @@
      * occurs then back off for a moment and try again. When a number of
      * attempts fail, give up and throw an exception.
      */
-    void createAFile(File aFile, List<String> contents) throws IOException {
+    void createAFile(File aFile, List<String> lines) throws IOException {
+        createAFile(aFile, lines, true);
+    }
+
+    void createAFile(File aFile, List<String> lines, boolean endWithNewline) throws IOException {
         IOException cause = null;
         for (int attempts = 0; attempts < 10; attempts++) {
             try {
-                Files.write(aFile.getAbsoluteFile().toPath(), contents,
-                    Charset.defaultCharset(), CREATE, TRUNCATE_EXISTING, WRITE);
+                if (endWithNewline) {
+                    Files.write(aFile.getAbsoluteFile().toPath(),
+                        lines, Charset.defaultCharset(),
+                        CREATE, TRUNCATE_EXISTING, WRITE);
+                } else {
+                    Files.write(aFile.getAbsoluteFile().toPath(),
+                        String.join(System.lineSeparator(), lines).getBytes(Charset.defaultCharset()),
+                        CREATE, TRUNCATE_EXISTING, WRITE);
+                }
                 if (cause != null) {
                     /*
                      * report attempts and errors that were encountered