[#1797] Throw IllegalArgumentException if AbstractByteBuf.skipBytes(...) is used with a negative value
This commit is contained in:
parent
5447fe1e59
commit
aed8c15326
@ -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",
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user