From dc816e9807e551f8a80027569407d4e812ad748b Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 3 Jun 2016 15:12:32 +0200 Subject: [PATCH] Ensure we null out cumulation buffer before fire through the pipeline in handlerRemoved(...) Motivation: We should ensure we null out the cumulation buffer before we fire it through the pipleine in handlerRemoved(...) as in theory it could be possible that another method is triggered as result of the fireChannelRead(...) or fireChannelReadComplete() that will try to access the cumulation. Modifications: Null out cumulation buffer early in handlerRemoved(...) Result: No possible to access the cumulation buffer that was already handed over. --- .../handler/codec/ByteToMessageDecoder.java | 27 +++++++++++-------- .../netty/handler/codec/ReplayingDecoder.java | 1 - 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java b/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java index 8d9f7deb8d..a4ccb5aee4 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java @@ -207,18 +207,23 @@ public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter @Override public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception { - ByteBuf buf = internalBuffer(); - int readable = buf.readableBytes(); - if (readable > 0) { - ByteBuf bytes = buf.readBytes(readable); - buf.release(); - ctx.fireChannelRead(bytes); - } else { - buf.release(); + ByteBuf buf = cumulation; + if (buf != null) { + // Directly set this to null so we are sure we not access it in any other method here anymore. + cumulation = null; + + int readable = buf.readableBytes(); + if (readable > 0) { + ByteBuf bytes = buf.readBytes(readable); + buf.release(); + ctx.fireChannelRead(bytes); + } else { + buf.release(); + } + + numReads = 0; + ctx.fireChannelReadComplete(); } - cumulation = null; - numReads = 0; - ctx.fireChannelReadComplete(); handlerRemoved0(ctx); } diff --git a/codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java b/codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java index 1fa42b242c..e3116b38af 100644 --- a/codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java @@ -21,7 +21,6 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPipeline; import io.netty.util.Signal; -import io.netty.util.internal.RecyclableArrayList; import io.netty.util.internal.StringUtil; import java.util.List;