Skip to content

Commit 94f8a74

Browse files
author
Ekaterina Vergizova
committed
8337506: Disable "best-fit" mapping on Windows command line
Backport-of: ff634a967027cfa56b666d31c45db9a4acc09ea4
1 parent c5b187d commit 94f8a74

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2018, 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
@@ -150,7 +150,25 @@ main(int argc, char **argv)
150150
}
151151
}
152152
}
153-
JLI_CmdToArgs(GetCommandLine());
153+
154+
// Obtain the command line in UTF-16, then convert it to ANSI code page
155+
// without the "best-fit" option
156+
LPWSTR wcCmdline = GetCommandLineW();
157+
int mbSize = WideCharToMultiByte(CP_ACP,
158+
WC_NO_BEST_FIT_CHARS | WC_COMPOSITECHECK | WC_DEFAULTCHAR,
159+
wcCmdline, -1, NULL, 0, NULL, NULL);
160+
// If the call to WideCharToMultiByte() fails, it returns 0, which
161+
// will then make the following JLI_MemAlloc() to issue exit(1)
162+
LPSTR mbCmdline = JLI_MemAlloc(mbSize);
163+
if (WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS | WC_COMPOSITECHECK | WC_DEFAULTCHAR,
164+
wcCmdline, -1, mbCmdline, mbSize, NULL, NULL) == 0) {
165+
perror("command line encoding conversion failure");
166+
exit(1);
167+
}
168+
169+
JLI_CmdToArgs(mbCmdline);
170+
JLI_MemFree(mbCmdline);
171+
154172
margc = JLI_GetStdArgc();
155173
// add one more to mark the end
156174
margv = (char **)JLI_MemAlloc((margc + 1) * (sizeof(char *)));
Lines changed: 83 additions & 0 deletions
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)