mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8222489: jcmd VM.system_properties gives unusable paths on Windows
Reviewed-by: sspitsyn, ysuenaga
This commit is contained in:
parent
5eef59d22d
commit
08c3b1fc8f
2 changed files with 110 additions and 9 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -26,8 +26,16 @@ package jdk.internal.vm;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.CharBuffer;
|
||||||
|
import java.nio.charset.CharacterCodingException;
|
||||||
|
import java.nio.charset.CoderResult;
|
||||||
|
import java.nio.charset.CodingErrorAction;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
import java.util.jar.Manifest;
|
import java.util.jar.Manifest;
|
||||||
import java.util.jar.Attributes;
|
import java.util.jar.Attributes;
|
||||||
|
@ -57,18 +65,52 @@ public class VMSupport {
|
||||||
*/
|
*/
|
||||||
private static byte[] serializePropertiesToByteArray(Properties p) throws IOException {
|
private static byte[] serializePropertiesToByteArray(Properties p) throws IOException {
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
|
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
|
||||||
|
PrintWriter bw = new PrintWriter(new OutputStreamWriter(out, "8859_1"));
|
||||||
|
|
||||||
Properties props = new Properties();
|
bw.println("#" + new Date().toString());
|
||||||
|
|
||||||
// stringPropertyNames() returns a snapshot of the property keys
|
try {
|
||||||
Set<String> keyset = p.stringPropertyNames();
|
for (String key : p.stringPropertyNames()) {
|
||||||
for (String key : keyset) {
|
String val = p.getProperty(key);
|
||||||
String value = p.getProperty(key);
|
key = toISO88591(toEscapeSpecialChar(toEscapeSpace(key)));
|
||||||
props.put(key, value);
|
val = toISO88591(toEscapeSpecialChar(val));
|
||||||
|
bw.println(key + "=" + val);
|
||||||
|
}
|
||||||
|
} catch (CharacterCodingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
bw.flush();
|
||||||
|
|
||||||
|
return out.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
props.store(out, null);
|
private static String toEscapeSpecialChar(String source) {
|
||||||
return out.toByteArray();
|
return source.replace("\t", "\\t").replace("\n", "\\n").replace("\r", "\\r").replace("\f", "\\f");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String toEscapeSpace(String source) {
|
||||||
|
return source.replace(" ", "\\ ");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String toISO88591(String source) throws CharacterCodingException {
|
||||||
|
var charBuf = CharBuffer.wrap(source);
|
||||||
|
// 6 is 2 bytes for '\\u' as String and 4 bytes for code point.
|
||||||
|
var byteBuf = ByteBuffer.allocate(charBuf.length() * 6);
|
||||||
|
var encoder = StandardCharsets.ISO_8859_1
|
||||||
|
.newEncoder()
|
||||||
|
.onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||||
|
|
||||||
|
CoderResult result;
|
||||||
|
do {
|
||||||
|
result = encoder.encode(charBuf, byteBuf, false);
|
||||||
|
if (result.isUnmappable()) {
|
||||||
|
byteBuf.put(String.format("\\u%04X", (int)charBuf.get()).getBytes());
|
||||||
|
} else if (result.isError()) {
|
||||||
|
result.throwException();
|
||||||
|
}
|
||||||
|
} while (result.isError());
|
||||||
|
|
||||||
|
return new String(byteBuf.array(), 0, byteBuf.position());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] serializePropertiesToByteArray() throws IOException {
|
public static byte[] serializePropertiesToByteArray() throws IOException {
|
||||||
|
|
59
test/jdk/sun/tools/jcmd/TestVM.java
Normal file
59
test/jdk/sun/tools/jcmd/TestVM.java
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import jdk.test.lib.process.OutputAnalyzer;
|
||||||
|
import jdk.test.lib.Platform;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @test
|
||||||
|
* @bug 8222489
|
||||||
|
* @summary Unit test for jcmd utility. The test will send different diagnostic
|
||||||
|
* command requests to the current java process.
|
||||||
|
*
|
||||||
|
* @library /test/lib
|
||||||
|
*
|
||||||
|
* @run main/othervm TestVM
|
||||||
|
*/
|
||||||
|
public class TestVM {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
System.setProperty("normal", "normal_val");
|
||||||
|
System.setProperty("nonascii", "\u3042\u3044\u3046\u3048\u304A");
|
||||||
|
System.setProperty("url", "http://openjdk.java.net/");
|
||||||
|
System.setProperty("winDir", "C:\\");
|
||||||
|
System.setProperty("mix", "a\u3044u\u3048\u304Aka");
|
||||||
|
System.setProperty("space space", "blank blank");
|
||||||
|
|
||||||
|
OutputAnalyzer output = JcmdBase.jcmd("VM.system_properties");
|
||||||
|
output.shouldNotContain("https\\:");
|
||||||
|
output.shouldNotContain("C\\:\\\\");
|
||||||
|
|
||||||
|
output.shouldContain("normal=normal_val");
|
||||||
|
output.shouldContain("nonascii=\\u3042\\u3044\\u3046\\u3048\\u304A");
|
||||||
|
output.shouldContain("url=http://openjdk.java.net/");
|
||||||
|
output.shouldContain("winDir=C:\\");
|
||||||
|
output.shouldContain("mix=a\\u3044u\\u3048\\u304Aka");
|
||||||
|
output.shouldContain("space\\ space=blank blank");
|
||||||
|
output.shouldContain(Platform.isWindows() ? "line.separator=\\r\\n" : "line.separator=\\n");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue