[#1797] Throw IllegalArgumentException if AbstractByteBuf.skipBytes(...) is used with a negative value

This commit is contained in:
Norman Maurer 2013-08-29 11:14:36 +02:00
parent 5447fe1e59
commit aed8c15326
2 changed files with 11 additions and 0 deletions

View File

@ -729,6 +729,8 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public ByteBuf skipBytes(int length) { public ByteBuf skipBytes(int length) {
checkReadableBytes(length);
int newReaderIndex = readerIndex + length; int newReaderIndex = readerIndex + length;
if (newReaderIndex > writerIndex) { if (newReaderIndex > writerIndex) {
throw new IndexOutOfBoundsException(String.format( throw new IndexOutOfBoundsException(String.format(
@ -1155,6 +1157,9 @@ public abstract class AbstractByteBuf extends ByteBuf {
*/ */
protected final void checkReadableBytes(int minimumReadableBytes) { protected final void checkReadableBytes(int minimumReadableBytes) {
ensureAccessible(); ensureAccessible();
if (minimumReadableBytes < 0) {
throw new IllegalArgumentException("minimumReadableBytes: " + minimumReadableBytes + " (expected: >= 0)");
}
if (readerIndex > writerIndex - minimumReadableBytes) { if (readerIndex > writerIndex - minimumReadableBytes) {
throw new IndexOutOfBoundsException(String.format( throw new IndexOutOfBoundsException(String.format(
"readerIndex(%d) + length(%d) exceeds writerIndex(%d): %s", "readerIndex(%d) + length(%d) exceeds writerIndex(%d): %s",

View File

@ -586,4 +586,10 @@ public class UnpooledTest {
} }
wrapped.release(); wrapped.release();
} }
@Test(expected = IllegalArgumentException.class)
public void skipBytesNegativeLength() {
ByteBuf buf = freeLater(buffer(8));
buf.skipBytes(-1);
}
} }