This is an automated email from the ASF dual-hosted git repository.

twolf pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git

commit 7fa206f51ff1e8ce784cb6a012adde1f9ada029c
Author: Thomas Wolf <tw...@apache.org>
AuthorDate: Sat Sep 7 10:14:51 2024 +0200

    GH-587: prevent NPE in NettyIoSession
    
    If a write occurs on an already closed channel, an NPE might occur.
    Handle this case by setting a ChannelClosedException on the returned
    future.
    
    This prevents the NPE, but other code may still issue other exceptions
    if a write on a closed channel occurs.
    
    Bug: https://github.com/apache/mina-sshd/issues/587
---
 CHANGES.md                                              |  1 +
 .../main/java/org/apache/sshd/netty/NettyIoSession.java | 17 +++++++++++++----
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index f640b886a..3361d80ac 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -41,6 +41,7 @@
 * [GH-524](https://github.com/apache/mina-sshd/issues/524) Performance 
improvements
 * [GH-533](https://github.com/apache/mina-sshd/issues/533) Fix multi-step 
authentication
 * [GH-582](https://github.com/apache/mina-sshd/issues/582) Fix filtering in 
`NamedFactory`
+* [GH-587](https://github.com/apache/mina-sshd/issues/587) Prevent 
`NullPointerException`on closed channel in `NettyIoSession`
 * [GH-590](https://github.com/apache/mina-sshd/issues/590) Better support for 
FIPS
 
 ## New Features
diff --git a/sshd-netty/src/main/java/org/apache/sshd/netty/NettyIoSession.java 
b/sshd-netty/src/main/java/org/apache/sshd/netty/NettyIoSession.java
index 8d90d6ce4..02c90ed56 100644
--- a/sshd-netty/src/main/java/org/apache/sshd/netty/NettyIoSession.java
+++ b/sshd-netty/src/main/java/org/apache/sshd/netty/NettyIoSession.java
@@ -21,6 +21,7 @@ package org.apache.sshd.netty;
 
 import java.io.IOException;
 import java.net.SocketAddress;
+import java.nio.channels.ClosedChannelException;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
@@ -134,11 +135,19 @@ public class NettyIoSession extends AbstractCloseable 
implements IoSession {
         ByteBuf buf = Unpooled.buffer(bufLen);
         buf.writeBytes(buffer.array(), buffer.rpos(), bufLen);
         DefaultIoWriteFuture msg = new 
DefaultIoWriteFuture(getRemoteAddress(), null);
-        ChannelPromise next = context.newPromise();
+        ChannelHandlerContext ctx = context;
+        if (ctx == null) {
+            msg.setValue(new ClosedChannelException());
+            return msg;
+        }
+        ChannelPromise next = ctx.newPromise();
         prev.addListener(whatever -> {
-            ChannelHandlerContext ctx = context;
-            if (ctx != null) {
-                ctx.writeAndFlush(buf, next);
+            ChannelHandlerContext c = context;
+            if (c != null) {
+                c.writeAndFlush(buf, next);
+            } else {
+                msg.setValue(new ClosedChannelException());
+                next.cancel(true);
             }
         });
         prev = next;

Reply via email to