Skip to content

Commit

Permalink
NetworkInterface.getByInetAddress() may return null on Android platfo…
Browse files Browse the repository at this point in the history
…rm (#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 #10045
  • Loading branch information
normanmaurer committed Feb 25, 2020
1 parent 914b433 commit 880e123
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion common/src/main/java/io/netty/util/internal/SocketUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collections;
import java.util.Enumeration;

/**
Expand All @@ -42,9 +43,16 @@
*/
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 {
Expand Down Expand Up @@ -176,12 +184,20 @@ public InetSocketAddress run() {
}

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")
Expand Down

0 comments on commit 880e123

Please sign in to comment.