NetworkInterface.getByInetAddress() may return null on Android platform (#10056)
Motivation: NetworkInterface.getByInetAddress() may return null on Android. This is incorrect by the API but still happens. To help our users we should provide a workaround Modifications: Just return an empty Enumeration when null is returned. Result: Fixes https://github.com/netty/netty/issues/10045
This commit is contained in:
parent
914b433a8d
commit
880e1239c5
@ -32,6 +32,7 @@ import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.security.PrivilegedActionException;
|
||||
import java.security.PrivilegedExceptionAction;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
|
||||
/**
|
||||
@ -42,9 +43,16 @@ import java.util.Enumeration;
|
||||
*/
|
||||
public final class SocketUtils {
|
||||
|
||||
private static final Enumeration<Object> EMPTY = Collections.enumeration(Collections.emptyList());
|
||||
|
||||
private SocketUtils() {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> Enumeration<T> empty() {
|
||||
return (Enumeration<T>) EMPTY;
|
||||
}
|
||||
|
||||
public static void connect(final Socket socket, final SocketAddress remoteAddress, final int timeout)
|
||||
throws IOException {
|
||||
try {
|
||||
@ -176,12 +184,20 @@ public final class SocketUtils {
|
||||
}
|
||||
|
||||
public static Enumeration<InetAddress> addressesFromNetworkInterface(final NetworkInterface intf) {
|
||||
return AccessController.doPrivileged(new PrivilegedAction<Enumeration<InetAddress>>() {
|
||||
Enumeration<InetAddress> addresses =
|
||||
AccessController.doPrivileged(new PrivilegedAction<Enumeration<InetAddress>>() {
|
||||
@Override
|
||||
public Enumeration<InetAddress> run() {
|
||||
return intf.getInetAddresses();
|
||||
}
|
||||
});
|
||||
// Android seems to sometimes return null even if this is not a valid return value by the api docs.
|
||||
// Just return an empty Enumeration in this case.
|
||||
// See https://github.com/netty/netty/issues/10045
|
||||
if (addresses == null) {
|
||||
return empty();
|
||||
}
|
||||
return addresses;
|
||||
}
|
||||
|
||||
@SuppressJava6Requirement(reason = "Usage guarded by java version check")
|
||||
|
Loading…
Reference in New Issue
Block a user