Skip to content

Commit 989455a

Browse files
author
duke
committedAug 6, 2024
Automatic merge of jdk:master into master
2 parents 067554d + ff634a9 commit 989455a

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed
 

‎src/java.base/share/native/launcher/main.c

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -110,7 +110,25 @@ main(int argc, char **argv)
110110
}
111111
}
112112
}
113-
JLI_CmdToArgs(GetCommandLine());
113+
114+
// Obtain the command line in UTF-16, then convert it to ANSI code page
115+
// without the "best-fit" option
116+
LPWSTR wcCmdline = GetCommandLineW();
117+
int mbSize = WideCharToMultiByte(CP_ACP,
118+
WC_NO_BEST_FIT_CHARS | WC_COMPOSITECHECK | WC_DEFAULTCHAR,
119+
wcCmdline, -1, NULL, 0, NULL, NULL);
120+
// If the call to WideCharToMultiByte() fails, it returns 0, which
121+
// will then make the following JLI_MemAlloc() to issue exit(1)
122+
LPSTR mbCmdline = JLI_MemAlloc(mbSize);
123+
if (WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS | WC_COMPOSITECHECK | WC_DEFAULTCHAR,
124+
wcCmdline, -1, mbCmdline, mbSize, NULL, NULL) == 0) {
125+
perror("command line encoding conversion failure");
126+
exit(1);
127+
}
128+
129+
JLI_CmdToArgs(mbCmdline);
130+
JLI_MemFree(mbCmdline);
131+
114132
margc = JLI_GetStdArgc();
115133
// add one more to mark the end
116134
margv = (char **)JLI_MemAlloc((margc + 1) * (sizeof(char *)));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2024, 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+
/*
25+
* @test
26+
* @bug 8337506
27+
* @summary Verify Command Line arguments are not mapped with
28+
* "best-fit" mappings on Windows
29+
* @requires (os.family == "windows")
30+
* @library /test/lib
31+
* @run junit DisableBestFitMappingTest
32+
*/
33+
import java.nio.ByteBuffer;
34+
import java.nio.charset.Charset;
35+
import java.nio.charset.CharsetEncoder;
36+
import java.util.stream.Stream;
37+
import jdk.test.lib.process.ProcessTools;
38+
39+
import org.junit.jupiter.api.Test;
40+
import org.junit.jupiter.params.ParameterizedTest;
41+
import org.junit.jupiter.params.provider.Arguments;
42+
import org.junit.jupiter.params.provider.MethodSource;
43+
44+
import static org.junit.jupiter.api.Assertions.*;
45+
import static org.junit.jupiter.api.Assumptions.*;
46+
47+
public class DisableBestFitMappingTest {
48+
private static final CharsetEncoder NATIVE_ENC =
49+
Charset.forName(System.getProperty("native.encoding")).newEncoder();
50+
private static final String REPLACEMENT =
51+
NATIVE_ENC.charset().decode(ByteBuffer.wrap(NATIVE_ENC.replacement())).toString();
52+
private static final int EXIT_SUCCESS = 0;
53+
private static final int EXIT_FAILURE = -1;
54+
55+
static Stream<Arguments> CMD_ARGS() {
56+
return Stream.of(
57+
Arguments.of("aa\uff02 \uff02bb", "aa" + REPLACEMENT + " " + REPLACEMENT + "bb"),
58+
Arguments.of("aa\uff01bb", "aa" + REPLACEMENT + "bb"),
59+
Arguments.of("aa\u221ebb", "aa" + REPLACEMENT + "bb")
60+
);
61+
}
62+
63+
@ParameterizedTest
64+
@MethodSource("CMD_ARGS")
65+
void testDisableBestFitMapping(String arg, String expected) throws Exception {
66+
// Only execute if the arg cannot be encoded
67+
assumeFalse(NATIVE_ENC.canEncode(arg),
68+
"native.encoding (%s) can encode the argument '%s'. Test ignored."
69+
.formatted(NATIVE_ENC.charset(), arg));
70+
71+
var result= ProcessTools.executeTestJava(
72+
DisableBestFitMappingTest.class.getSimpleName(), arg, expected);
73+
result.asLines().forEach(System.out::println);
74+
assertEquals(EXIT_SUCCESS, result.getExitValue(),
75+
"Disabling best-fit mapping failed");
76+
}
77+
78+
public static void main(String... args) {
79+
System.out.println(args[0]);
80+
System.out.println(args[1]);
81+
System.exit(args[0].equals(args[1]) ? EXIT_SUCCESS : EXIT_FAILURE);
82+
}
83+
}

0 commit comments

Comments
 (0)
Please sign in to comment.