Fix NPE when remote address can not be obtained

Motivation:

In the native transport we use getpeername to obtain the remote address from the file descriptor. This may fail for various reasons in which case NULL is returned.

Modifications:

- Check for null when try to obtain remote / local address

Result:

No more NPE
This commit is contained in:
Norman Maurer 2015-01-17 20:30:18 +01:00
parent 8614b88c18
commit bc97d7d7a1
2 changed files with 18 additions and 0 deletions

View File

@ -94,6 +94,14 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So
@Override @Override
protected SocketAddress remoteAddress0() { protected SocketAddress remoteAddress0() {
if (remote == null) {
// Remote address not know, try to get it now.
InetSocketAddress address = Native.remoteAddress(fd);
if (address != null) {
remote = address;
}
return address;
}
return remote; return remote;
} }

View File

@ -399,11 +399,21 @@ final class Native {
public static InetSocketAddress remoteAddress(int fd) { public static InetSocketAddress remoteAddress(int fd) {
byte[] addr = remoteAddress0(fd); byte[] addr = remoteAddress0(fd);
// addr may be null if getpeername failed.
// See https://github.com/netty/netty/issues/3328
if (addr == null) {
return null;
}
return address(addr); return address(addr);
} }
public static InetSocketAddress localAddress(int fd) { public static InetSocketAddress localAddress(int fd) {
byte[] addr = localAddress0(fd); byte[] addr = localAddress0(fd);
// addr may be null if getpeername failed.
// See https://github.com/netty/netty/issues/3328
if (addr == null) {
return null;
}
return address(addr); return address(addr);
} }