From 0d58d07bbf043d69912e5f30ae8bcb9446f02aaa Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sat, 17 Jan 2015 20:30:18 +0100 Subject: [PATCH] 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 --- .../io/netty/channel/epoll/EpollSocketChannel.java | 8 ++++++++ .../src/main/java/io/netty/channel/epoll/Native.java | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java index bfe1f52869..be691b5e6f 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java @@ -94,6 +94,14 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So @Override 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; } diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/Native.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/Native.java index 4986e3f20b..762c91d9c4 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/Native.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/Native.java @@ -399,11 +399,21 @@ final class Native { public static InetSocketAddress remoteAddress(int 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); } public static InetSocketAddress localAddress(int 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); }