Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8222489: jcmd VM.system_properties gives unusable paths on Windows
Reviewed-by: sspitsyn, ysuenaga
  • Loading branch information
chiroito committed Mar 7, 2020
1 parent 5eef59d commit 08c3b1fc8f9aa5c040ecdfd9e3b14150b22a27f7
Showing with 110 additions and 9 deletions.
  1. +51 −9 src/java.base/share/classes/jdk/internal/vm/VMSupport.java
  2. +59 −0 test/jdk/sun/tools/jcmd/TestVM.java
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@@ -26,8 +26,16 @@

import java.io.ByteArrayOutputStream;
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.Set;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.jar.Attributes;
@@ -57,20 +65,54 @@ public static synchronized Properties getAgentProperties() {
*/
private static byte[] serializePropertiesToByteArray(Properties p) throws IOException {
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
Set<String> keyset = p.stringPropertyNames();
for (String key : keyset) {
String value = p.getProperty(key);
props.put(key, value);
try {
for (String key : p.stringPropertyNames()) {
String val = p.getProperty(key);
key = toISO88591(toEscapeSpecialChar(toEscapeSpace(key)));
val = toISO88591(toEscapeSpecialChar(val));
bw.println(key + "=" + val);
}
} catch (CharacterCodingException e) {
throw new RuntimeException(e);
}
bw.flush();

props.store(out, null);
return out.toByteArray();
}

private static String toEscapeSpecialChar(String source) {
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 {
return serializePropertiesToByteArray(System.getProperties());
}
@@ -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");
}
}

0 comments on commit 08c3b1f

Please sign in to comment.