From da1418e269e8a5af1c32e462dfe4385a7570ee2e Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 16 Oct 2013 09:53:26 +0200 Subject: [PATCH] [#1920] Fix IndexOutOfBoundsException when using PooledByteBufAllocator with SCTP and NIO Datagram channels --- .../java/io/netty/channel/sctp/nio/NioSctpChannel.java | 8 ++++---- .../io/netty/channel/socket/nio/NioDatagramChannel.java | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpChannel.java b/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpChannel.java index cd210cfd9e..f2ec013a01 100644 --- a/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpChannel.java +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpChannel.java @@ -274,14 +274,14 @@ public class NioSctpChannel extends AbstractNioMessageChannel implements io.nett ByteBuf buffer = allocHandle.allocate(config().getAllocator()); boolean free = true; try { - ByteBuffer data = buffer.nioBuffer(buffer.writerIndex(), buffer.writableBytes()); + ByteBuffer data = buffer.internalNioBuffer(buffer.writerIndex(), buffer.writableBytes()); + int pos = data.position(); + MessageInfo messageInfo = ch.receive(data, null, notificationHandler); if (messageInfo == null) { return 0; } - - data.flip(); - buf.add(new SctpMessage(messageInfo, buffer.writerIndex(buffer.writerIndex() + data.remaining()))); + buf.add(new SctpMessage(messageInfo, buffer.writerIndex(buffer.writerIndex() + (data.position() - pos)))); free = false; return 1; } catch (Throwable cause) { diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java index aaac3f6569..b3eb5a21db 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java @@ -205,14 +205,14 @@ public final class NioDatagramChannel ByteBuf data = allocHandle.allocate(config.getAllocator()); boolean free = true; try { - ByteBuffer nioData = data.nioBuffer(data.writerIndex(), data.writableBytes()); - + ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes()); + int pos = nioData.position(); InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData); if (remoteAddress == null) { return 0; } - int readBytes = nioData.position(); + int readBytes = nioData.position() - pos; data.writerIndex(data.writerIndex() + readBytes); allocHandle.record(readBytes);