8344857: Remove calls to SecurityManager and doPrivileged in SocketExceptions and URLJarFile in the sun.net package after JEP 486 integration

Reviewed-by: dfuchs, michaelm
This commit is contained in:
Jaikiran Pai 2024-11-26 00:52:50 +00:00
parent 48e3b6511a
commit 3326874f5f
2 changed files with 26 additions and 50 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2024, 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
@ -30,8 +30,6 @@ import java.lang.reflect.Constructor;
import java.net.InetSocketAddress;
import java.net.UnixDomainSocketAddress;
import java.net.SocketAddress;
import java.security.AccessController;
import java.security.PrivilegedAction;
import sun.security.util.SecurityProperties;
@ -83,22 +81,16 @@ public final class SocketExceptions {
// return a new instance of the same type with the given detail
// msg, or if the type doesn't support detail msgs, return given
// instance.
@SuppressWarnings("removal")
private static IOException create(IOException e, String msg) {
return AccessController.doPrivileged(new PrivilegedAction<IOException>() {
public IOException run() {
try {
Class<?> clazz = e.getClass();
Constructor<?> ctor = clazz.getConstructor(String.class);
IOException e1 = (IOException)(ctor.newInstance(msg));
e1.setStackTrace(e.getStackTrace());
return e1;
} catch (Exception e0) {
// Some eg AsynchronousCloseException have no detail msg
return e;
}
}
});
private static IOException create(final IOException e, final String msg) {
try {
Class<?> clazz = e.getClass();
Constructor<?> ctor = clazz.getConstructor(String.class);
IOException e1 = (IOException)(ctor.newInstance(msg));
e1.setStackTrace(e.getStackTrace());
return e1;
} catch (Exception e0) {
// Some eg AsynchronousCloseException have no detail msg
return e;
}
}
}

View File

@ -36,9 +36,6 @@ import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.security.CodeSigner;
import java.security.cert.Certificate;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.security.PrivilegedActionException;
import sun.net.www.ParseUtil;
/* URL jar file is a common JarFile subtype used for JarURLConnection */
@ -159,39 +156,26 @@ public class URLJarFile extends JarFile {
* Given a URL, retrieves a JAR file, caches it to disk, and creates a
* cached JAR file object.
*/
@SuppressWarnings("removal")
private static JarFile retrieve(final URL url, final URLJarFileCloseController closeController) throws IOException {
JarFile result = null;
Runtime.Version version = "runtime".equals(url.getRef())
? JarFile.runtimeVersion()
: JarFile.baseVersion();
/* get the stream before asserting privileges */
try (final InputStream in = url.openConnection().getInputStream()) {
result = AccessController.doPrivileged(
new PrivilegedExceptionAction<>() {
public JarFile run() throws IOException {
Path tmpFile = Files.createTempFile("jar_cache", null);
try {
Files.copy(in, tmpFile, StandardCopyOption.REPLACE_EXISTING);
JarFile jarFile = new URLJarFile(tmpFile.toFile(), closeController, version);
tmpFile.toFile().deleteOnExit();
return jarFile;
} catch (Throwable thr) {
try {
Files.delete(tmpFile);
} catch (IOException ioe) {
thr.addSuppressed(ioe);
}
throw thr;
}
}
});
} catch (PrivilegedActionException pae) {
throw (IOException) pae.getException();
Path tmpFile = Files.createTempFile("jar_cache", null);
try {
Files.copy(in, tmpFile, StandardCopyOption.REPLACE_EXISTING);
JarFile jarFile = new URLJarFile(tmpFile.toFile(), closeController, version);
tmpFile.toFile().deleteOnExit();
return jarFile;
} catch (Throwable thr) {
try {
Files.delete(tmpFile);
} catch (IOException ioe) {
thr.addSuppressed(ioe);
}
throw thr;
}
}
return result;
}
private class URLJarFileEntry extends JarEntry {