8275007: Java fails to start with null charset if LC_ALL is set to certain locales

Reviewed-by: rriggs, iris, joehw, alanb
This commit is contained in:
Naoto Sato 2021-11-18 01:13:26 +00:00
parent 231fb61aae
commit b8453ebdb4
2 changed files with 61 additions and 63 deletions

View file

@ -33,7 +33,6 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
@ -45,9 +44,9 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URL;
import java.nio.charset.CharacterCodingException;
import java.nio.channels.Channel;
import java.nio.channels.spi.SelectorProvider;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.security.AccessControlContext;
import java.security.AccessController;
@ -84,6 +83,7 @@ import jdk.internal.vm.annotation.Stable;
import sun.nio.fs.DefaultFileSystemProvider;
import sun.reflect.annotation.AnnotationType;
import sun.nio.ch.Interruptible;
import sun.nio.cs.UTF_8;
import sun.security.util.SecurityConstants;
/**
@ -188,6 +188,11 @@ public final class System {
@SuppressWarnings("removal")
private static volatile SecurityManager security; // read by VM
// `sun.jnu.encoding` if it is not supported. Otherwise null.
// It is initialized in `initPhase1()` before any charset providers
// are initialized.
private static String notSupportedJnuEncoding;
// return true if a security manager is allowed
private static boolean allowSecurityManager() {
return (allowSecurityManager != NEVER);
@ -2017,10 +2022,9 @@ public final class System {
* Create PrintStream for stdout/err based on encoding.
*/
private static PrintStream newPrintStream(FileOutputStream fos, String enc) {
if (enc != null) {
try {
return new PrintStream(new BufferedOutputStream(fos, 128), true, enc);
} catch (UnsupportedEncodingException uee) {}
if (enc != null) {
return new PrintStream(new BufferedOutputStream(fos, 128), true,
Charset.forName(enc, UTF_8.INSTANCE));
}
return new PrintStream(new BufferedOutputStream(fos, 128), true);
}
@ -2113,6 +2117,13 @@ public final class System {
VM.saveProperties(tempProps);
props = createProperties(tempProps);
// Check if sun.jnu.encoding is supported. If not, replace it with UTF-8.
var jnuEncoding = props.getProperty("sun.jnu.encoding");
if (jnuEncoding == null || !Charset.isSupported(jnuEncoding)) {
notSupportedJnuEncoding = jnuEncoding == null ? "null" : jnuEncoding;
props.setProperty("sun.jnu.encoding", "UTF-8");
}
StaticProperty.javaHome(); // Load StaticProperty to cache the property values
lineSeparator = props.getProperty("line.separator");
@ -2141,7 +2152,6 @@ public final class System {
Thread current = Thread.currentThread();
current.getThreadGroup().add(current);
// Subsystems that are invoked during initialization can invoke
// VM.isBooted() in order to avoid doing things that should
// wait until the VM is fully initialized. The initialization level
@ -2248,6 +2258,14 @@ public final class System {
WARNING: The Security Manager is deprecated and will be removed in a future release""");
}
// Emit a warning if `sun.jnu.encoding` is not supported.
if (notSupportedJnuEncoding != null) {
System.err.println(
"WARNING: The encoding of the underlying platform's" +
" file system is not supported: " +
notSupportedJnuEncoding);
}
initialErrStream = System.err;
// initializing the system class loader