Skip to content

Commit

Permalink
GH-587: prevent NPE in NettyIoSession
Browse files Browse the repository at this point in the history
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: #587
  • Loading branch information
tomaswolf committed Sep 7, 2024
1 parent f039b68 commit 7fa206f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions sshd-netty/src/main/java/org/apache/sshd/netty/NettyIoSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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;
Expand Down Expand Up @@ -134,11 +135,19 @@ public IoWriteFuture writeBuffer(Buffer buffer) {
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;
Expand Down

0 comments on commit 7fa206f

Please sign in to comment.