Skip to content

Commit 0b62553

Browse files
committed
8220700: jlink generated launcher script needs quoting to avoid parameter expansion
Reviewed-by: mchung, alanb, sgehwolf
1 parent 444e784 commit 0b62553

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

src/jdk.jlink/share/classes/jdk/tools/jlink/builder/DefaultImageBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ protected void prepareApplicationFiles(ResourcePool imageContent) throws IOExcep
300300
sb.append("$DIR/java $JLINK_VM_OPTIONS -m ")
301301
.append(module).append('/')
302302
.append(mainClassName)
303-
.append(" $@\n");
303+
.append(" \"$@\"\n");
304304

305305
try (BufferedWriter writer = Files.newBufferedWriter(cmd,
306306
StandardCharsets.ISO_8859_1,

test/jdk/tools/jlink/basic/BasicTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,35 @@ public void run() throws Throwable {
100100
runJmod(classes.toString(), TEST_MODULE, true);
101101
runJlink(image, TEST_MODULE, "--launcher", "bar=" + TEST_MODULE);
102102
execute(image, "bar");
103-
104103
Files.delete(jmods.resolve(TEST_MODULE + ".jmod"));
105104

106105
image = Paths.get("myimage2");
107106
runJmod(classes.toString(), TEST_MODULE, false /* no ModuleMainClass! */);
108107
// specify main class in --launcher command line
109108
runJlink(image, TEST_MODULE, "--launcher", "bar2=" + TEST_MODULE + "/jdk.test.Test");
110109
execute(image, "bar2");
110+
Files.delete(jmods.resolve(TEST_MODULE + ".jmod"));
111+
112+
image = Paths.get("myadder");
113+
runJmod(classes.toString(), TEST_MODULE, false /* no ModuleMainClass! */);
114+
// specify main class in --launcher command line
115+
runJlink(image, TEST_MODULE, "--launcher", "adder=" + TEST_MODULE + "/jdk.test.Adder");
116+
addAndCheck(image, "adder");
117+
}
111118

119+
private void addAndCheck(Path image, String scriptName) throws Throwable {
120+
String cmd = image.resolve("bin").resolve(scriptName).toString();
121+
OutputAnalyzer analyzer;
122+
if (System.getProperty("os.name").startsWith("Windows")) {
123+
analyzer = ProcessTools.executeProcess("sh.exe", cmd, "12", "8", "7", "--", "foo bar");
124+
} else {
125+
analyzer = ProcessTools.executeProcess(cmd, "12", "8", "7", "--", "foo bar");
126+
}
127+
if (analyzer.getExitValue() != 27) {
128+
throw new AssertionError("Image invocation failed: expected 27, rc=" + analyzer.getExitValue());
129+
}
130+
// last argument contains space and should be properly quoted.
131+
analyzer.stdoutShouldContain("Num args: 5");
112132
}
113133

114134
private void execute(Path image, String scriptName) throws Throwable {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package jdk.test;
25+
26+
import java.util.Arrays;
27+
28+
public class Adder {
29+
public static void main(String[] args) {
30+
System.out.println(Adder.class + " ...");
31+
System.out.println("Num args: " + args.length);
32+
System.out.println("args list: " + Arrays.asList(args));
33+
int sum = 0;
34+
// Only add arguments upto "--". The remaining argument(s) are for
35+
// testing quoting.
36+
for (String arg: args) {
37+
System.out.println(arg);
38+
if ("--".equals(arg)) {
39+
break;
40+
}
41+
sum += Integer.parseInt(arg);
42+
}
43+
System.exit(sum); // checked by the test
44+
}
45+
}

0 commit comments

Comments
 (0)