[#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 51a536fd30
commit 5ddd7cee90
2 changed files with 11 additions and 0 deletions

View File

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

View File

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