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:
parent
8614b88c18
commit
bc97d7d7a1
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user