From 548540bc2de34d776206f32f15218bdcadb36c19 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 21 May 2013 20:19:00 +0200 Subject: [PATCH] Fix a which could cause data corruption when using AioSocketChannel. This was because it was possible to have the JDK read into a wrong buffer region if the user called discardReadBytes() later. Fixes #1377 --- .../java/io/netty/channel/socket/aio/AioSocketChannel.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java index c954eaf176..5de00a9a11 100755 --- a/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java @@ -336,6 +336,13 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne } ByteBuf byteBuf = pipeline().inboundByteBuffer(); + + // Ensure the readerIndex of the buffer is 0 before beginning an async read. + // Otherwise, JDK can read into a wrong region of the buffer when a handler calls + // discardReadBytes() later, modifying the readerIndex and the writerIndex unexpectedly. + // See https://github.com/netty/netty/issues/1377 + byteBuf.discardReadBytes(); + expandReadBuffer(byteBuf); readInProgress = true;