8344218: Remove calls to SecurityManager and and doPrivileged in java.net.NetworkInterface after JEP 486 integration

Reviewed-by: dfuchs
This commit is contained in:
Eirik Bjørsnøs 2024-11-19 05:44:30 +00:00
parent d85dd77edf
commit b12c5b4d18

View File

@ -27,6 +27,7 @@ package java.net;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Spliterator;
@ -79,9 +80,9 @@ public final class NetworkInterface {
private String name;
private String displayName;
private int index;
private InetAddress addrs[];
private InterfaceAddress bindings[];
private NetworkInterface childs[];
private InetAddress[] addrs;
private InterfaceAddress[] bindings;
private NetworkInterface[] childs;
private NetworkInterface parent = null;
private boolean virtual = false;
private static final NetworkInterface defaultInterface;
@ -118,87 +119,46 @@ public final class NetworkInterface {
}
/**
* Get an Enumeration with all, or a subset, of the InetAddresses bound to
* this network interface.
* Get an Enumeration of the InetAddresses bound to this network interface.
*
* @implNote
* The returned enumeration contains all, or a subset, of the InetAddresses that were
* bound to the interface at the time the {@linkplain #getNetworkInterfaces()
* The returned enumeration contains the InetAddresses that were bound to
* the interface at the time the {@linkplain #getNetworkInterfaces()
* interface configuration was read}
*
* @return an Enumeration object with all, or a subset, of the InetAddresses
* bound to this network interface
* @return an Enumeration object with the InetAddresses bound to this
* network interface
* @see #inetAddresses()
*/
public Enumeration<InetAddress> getInetAddresses() {
return enumerationFromArray(getCheckedInetAddresses());
return enumerationFromArray(addrs);
}
/**
* Get a Stream of all, or a subset, of the InetAddresses bound to this
* network interface.
* Get a Stream of the InetAddresses bound to this network interface.
*
* @implNote
* The stream contains all, or a subset, of the InetAddresses that were
* bound to the interface at the time the {@linkplain #getNetworkInterfaces()
* The stream contains the InetAddresses that were bound to the
* interface at the time the {@linkplain #getNetworkInterfaces()
* interface configuration was read}
*
* @return a Stream object with all, or a subset, of the InetAddresses
* bound to this network interface
* @return a Stream object with the InetAddresses bound to this network interface
* @since 9
*/
public Stream<InetAddress> inetAddresses() {
return streamFromArray(getCheckedInetAddresses());
}
private InetAddress[] getCheckedInetAddresses() {
InetAddress[] local_addrs = new InetAddress[addrs.length];
boolean trusted = true;
@SuppressWarnings("removal")
SecurityManager sec = System.getSecurityManager();
if (sec != null) {
try {
sec.checkPermission(new NetPermission("getNetworkInformation"));
} catch (SecurityException e) {
trusted = false;
}
}
int i = 0;
for (int j = 0; j < addrs.length; j++) {
try {
if (!trusted) {
sec.checkConnect(addrs[j].getHostAddress(), -1);
}
local_addrs[i++] = addrs[j];
} catch (SecurityException e) { }
}
return Arrays.copyOf(local_addrs, i);
return streamFromArray(addrs);
}
/**
* Get a List of all, or a subset, of the {@code InterfaceAddresses}
* of this network interface.
* Get a List of the {@code InterfaceAddresses} of this network interface.
*
* @return a {@code List} object with the InterfaceAddress of this
* network interface
*
* @return a {@code List} object with all, or a subset, of the
* InterfaceAddress of this network interface
* @since 1.6
*/
public java.util.List<InterfaceAddress> getInterfaceAddresses() {
java.util.List<InterfaceAddress> lst = new java.util.ArrayList<>(1);
if (bindings != null) {
@SuppressWarnings("removal")
SecurityManager sec = System.getSecurityManager();
for (int j=0; j<bindings.length; j++) {
try {
if (sec != null) {
sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1);
}
lst.add(bindings[j]);
} catch (SecurityException e) { }
}
}
return lst;
public List<InterfaceAddress> getInterfaceAddresses() {
return bindings == null ? List.of() : List.of(bindings);
}
/**
@ -556,18 +516,6 @@ public final class NetworkInterface {
* @since 1.6
*/
public byte[] getHardwareAddress() throws SocketException {
@SuppressWarnings("removal")
SecurityManager sec = System.getSecurityManager();
if (sec != null) {
try {
sec.checkPermission(new NetPermission("getNetworkInformation"));
} catch (SecurityException e) {
if (!getInetAddresses().hasMoreElements()) {
// don't have connect permission to any local address
return null;
}
}
}
if (isLoopback0(name, index)) {
return null;
}