8285517: System.getenv() returns unexpected value if environment variable has non ASCII character

Reviewed-by: naoto, rriggs
This commit is contained in:
Ichiroh Takiguchi 2022-05-19 23:38:15 +00:00
parent de74e0e25a
commit 890771e708
5 changed files with 240 additions and 47 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, 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
@ -55,7 +55,9 @@
package java.lang;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import jdk.internal.util.StaticProperty;
final class ProcessEnvironment
@ -163,7 +165,7 @@ final class ProcessEnvironment
}
public static Variable valueOfQueryOnly(String str) {
return new Variable(str, str.getBytes());
return new Variable(str, str.getBytes(StaticProperty.jnuCharset()));
}
public static Variable valueOf(String str) {
@ -172,7 +174,7 @@ final class ProcessEnvironment
}
public static Variable valueOf(byte[] bytes) {
return new Variable(new String(bytes), bytes);
return new Variable(new String(bytes, StaticProperty.jnuCharset()), bytes);
}
public int compareTo(Variable variable) {
@ -196,7 +198,7 @@ final class ProcessEnvironment
}
public static Value valueOfQueryOnly(String str) {
return new Value(str, str.getBytes());
return new Value(str, str.getBytes(StaticProperty.jnuCharset()));
}
public static Value valueOf(String str) {
@ -205,7 +207,7 @@ final class ProcessEnvironment
}
public static Value valueOf(byte[] bytes) {
return new Value(new String(bytes), bytes);
return new Value(new String(bytes, StaticProperty.jnuCharset()), bytes);
}
public int compareTo(Value value) {

View file

@ -35,6 +35,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Locale;
@ -151,7 +152,7 @@ final class ProcessImpl extends Process {
private static byte[] toCString(String s) {
if (s == null)
return null;
byte[] bytes = s.getBytes();
byte[] bytes = s.getBytes(StaticProperty.jnuCharset());
byte[] result = new byte[bytes.length + 1];
System.arraycopy(bytes, 0,
result, 0,
@ -175,7 +176,7 @@ final class ProcessImpl extends Process {
byte[][] args = new byte[cmdarray.length-1][];
int size = args.length; // For added NUL bytes
for (int i = 0; i < args.length; i++) {
args[i] = cmdarray[i+1].getBytes();
args[i] = cmdarray[i+1].getBytes(StaticProperty.jnuCharset());
size += args[i].length;
}
byte[] argBlock = new byte[size];