From 512908f993d183cc2a7d8f8f9ebec44d174b7488 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 30 Sep 2013 14:47:49 +0200 Subject: [PATCH] [#1875] Correctly check the readerIndex when try to read a byte from AbstractByteBuf --- buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java | 1 + .../src/test/java/io/netty/buffer/AbstractByteBufTest.java | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java index e44a61a6fc..e466407f07 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java @@ -559,6 +559,7 @@ public abstract class AbstractByteBuf extends ByteBuf { @Override public byte readByte() { + checkReadableBytes(1); int i = readerIndex; byte b = getByte(i); readerIndex = i + 1; diff --git a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java index 4d33483ce2..d9ecbb7824 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java @@ -1935,6 +1935,13 @@ public abstract class AbstractByteBufTest { assertNull(cause.get()); } + @Test(expected = IndexOutOfBoundsException.class) + public void readByteThrowsIndexOutOfBoundsException() { + final ByteBuf buffer = freeLater(newBuffer(8)); + buffer.writeByte(0); + assertEquals((byte) 0 , buffer.readByte()); + buffer.readByte(); + } static final class TestGatheringByteChannel implements GatheringByteChannel { private final ByteArrayOutputStream out = new ByteArrayOutputStream(); private final WritableByteChannel channel = Channels.newChannel(out);