Skip to content

Commit b12c5b4

Browse files
author
Eirik Bjørsnøs
committed
8344218: Remove calls to SecurityManager and and doPrivileged in java.net.NetworkInterface after JEP 486 integration
Reviewed-by: dfuchs
1 parent d85dd77 commit b12c5b4

File tree

1 file changed

+21
-73
lines changed

1 file changed

+21
-73
lines changed

src/java.base/share/classes/java/net/NetworkInterface.java

Lines changed: 21 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.util.Arrays;
2929
import java.util.Enumeration;
30+
import java.util.List;
3031
import java.util.NoSuchElementException;
3132
import java.util.Objects;
3233
import java.util.Spliterator;
@@ -79,9 +80,9 @@ public final class NetworkInterface {
7980
private String name;
8081
private String displayName;
8182
private int index;
82-
private InetAddress addrs[];
83-
private InterfaceAddress bindings[];
84-
private NetworkInterface childs[];
83+
private InetAddress[] addrs;
84+
private InterfaceAddress[] bindings;
85+
private NetworkInterface[] childs;
8586
private NetworkInterface parent = null;
8687
private boolean virtual = false;
8788
private static final NetworkInterface defaultInterface;
@@ -118,87 +119,46 @@ public String getName() {
118119
}
119120

120121
/**
121-
* Get an Enumeration with all, or a subset, of the InetAddresses bound to
122-
* this network interface.
122+
* Get an Enumeration of the InetAddresses bound to this network interface.
123123
*
124124
* @implNote
125-
* The returned enumeration contains all, or a subset, of the InetAddresses that were
126-
* bound to the interface at the time the {@linkplain #getNetworkInterfaces()
125+
* The returned enumeration contains the InetAddresses that were bound to
126+
* the interface at the time the {@linkplain #getNetworkInterfaces()
127127
* interface configuration was read}
128128
*
129-
* @return an Enumeration object with all, or a subset, of the InetAddresses
130-
* bound to this network interface
129+
* @return an Enumeration object with the InetAddresses bound to this
130+
* network interface
131131
* @see #inetAddresses()
132132
*/
133133
public Enumeration<InetAddress> getInetAddresses() {
134-
return enumerationFromArray(getCheckedInetAddresses());
134+
return enumerationFromArray(addrs);
135135
}
136136

137137
/**
138-
* Get a Stream of all, or a subset, of the InetAddresses bound to this
139-
* network interface.
138+
* Get a Stream of the InetAddresses bound to this network interface.
140139
*
141140
* @implNote
142-
* The stream contains all, or a subset, of the InetAddresses that were
143-
* bound to the interface at the time the {@linkplain #getNetworkInterfaces()
141+
* The stream contains the InetAddresses that were bound to the
142+
* interface at the time the {@linkplain #getNetworkInterfaces()
144143
* interface configuration was read}
145144
*
146-
* @return a Stream object with all, or a subset, of the InetAddresses
147-
* bound to this network interface
145+
* @return a Stream object with the InetAddresses bound to this network interface
148146
* @since 9
149147
*/
150148
public Stream<InetAddress> inetAddresses() {
151-
return streamFromArray(getCheckedInetAddresses());
152-
}
153-
154-
private InetAddress[] getCheckedInetAddresses() {
155-
InetAddress[] local_addrs = new InetAddress[addrs.length];
156-
boolean trusted = true;
157-
158-
@SuppressWarnings("removal")
159-
SecurityManager sec = System.getSecurityManager();
160-
if (sec != null) {
161-
try {
162-
sec.checkPermission(new NetPermission("getNetworkInformation"));
163-
} catch (SecurityException e) {
164-
trusted = false;
165-
}
166-
}
167-
int i = 0;
168-
for (int j = 0; j < addrs.length; j++) {
169-
try {
170-
if (!trusted) {
171-
sec.checkConnect(addrs[j].getHostAddress(), -1);
172-
}
173-
local_addrs[i++] = addrs[j];
174-
} catch (SecurityException e) { }
175-
}
176-
return Arrays.copyOf(local_addrs, i);
149+
return streamFromArray(addrs);
177150
}
178151

179152
/**
180-
* Get a List of all, or a subset, of the {@code InterfaceAddresses}
181-
* of this network interface.
153+
* Get a List of the {@code InterfaceAddresses} of this network interface.
154+
*
155+
* @return a {@code List} object with the InterfaceAddress of this
156+
* network interface
182157
*
183-
* @return a {@code List} object with all, or a subset, of the
184-
* InterfaceAddress of this network interface
185158
* @since 1.6
186159
*/
187-
public java.util.List<InterfaceAddress> getInterfaceAddresses() {
188-
java.util.List<InterfaceAddress> lst = new java.util.ArrayList<>(1);
189-
if (bindings != null) {
190-
@SuppressWarnings("removal")
191-
SecurityManager sec = System.getSecurityManager();
192-
for (int j=0; j<bindings.length; j++) {
193-
try {
194-
if (sec != null) {
195-
sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1);
196-
}
197-
lst.add(bindings[j]);
198-
} catch (SecurityException e) { }
199-
}
200-
}
201-
return lst;
160+
public List<InterfaceAddress> getInterfaceAddresses() {
161+
return bindings == null ? List.of() : List.of(bindings);
202162
}
203163

204164
/**
@@ -556,18 +516,6 @@ public boolean supportsMulticast() throws SocketException {
556516
* @since 1.6
557517
*/
558518
public byte[] getHardwareAddress() throws SocketException {
559-
@SuppressWarnings("removal")
560-
SecurityManager sec = System.getSecurityManager();
561-
if (sec != null) {
562-
try {
563-
sec.checkPermission(new NetPermission("getNetworkInformation"));
564-
} catch (SecurityException e) {
565-
if (!getInetAddresses().hasMoreElements()) {
566-
// don't have connect permission to any local address
567-
return null;
568-
}
569-
}
570-
}
571519
if (isLoopback0(name, index)) {
572520
return null;
573521
}

0 commit comments

Comments
 (0)