classlib: fix ByteBuffer,CharBuffer,*Buffer IOOB exception throw with zero length arrays in arguments

fix #713
This commit is contained in:
Kirill Prazdnikov 2023-06-21 16:30:30 +03:00 committed by GitHub
parent 071a5d90fb
commit 81124a084b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 64 additions and 15 deletions

View File

@ -67,7 +67,7 @@ public abstract class TByteBuffer extends TBuffer implements TComparable<TByteBu
public abstract TByteBuffer put(int index, byte b);
public TByteBuffer get(byte[] dst, int offset, int length) {
if (offset < 0 || offset >= dst.length) {
if (offset < 0 || offset > dst.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")");
}
if (offset + length > dst.length) {
@ -106,7 +106,7 @@ public abstract class TByteBuffer extends TBuffer implements TComparable<TByteBu
if (remaining() < length) {
throw new TBufferOverflowException();
}
if (offset < 0 || offset >= src.length) {
if (offset < 0 || offset > src.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + src.length + ")");
}
if (offset + length > src.length) {

View File

@ -89,7 +89,7 @@ public abstract class TCharBuffer extends TBuffer implements Comparable<TCharBuf
public abstract TCharBuffer put(int index, char c);
public TCharBuffer get(char[] dst, int offset, int length) {
if (offset < 0 || offset >= dst.length) {
if (offset < 0 || offset > dst.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")");
}
if (offset + length > dst.length) {
@ -138,7 +138,7 @@ public abstract class TCharBuffer extends TBuffer implements Comparable<TCharBuf
if (remaining() < length) {
throw new TBufferOverflowException();
}
if (offset < 0 || offset >= src.length) {
if (offset < 0 || offset > src.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + src.length + ")");
}
if (offset + length > src.length) {
@ -168,7 +168,7 @@ public abstract class TCharBuffer extends TBuffer implements Comparable<TCharBuf
if (remaining() < sz) {
throw new TBufferOverflowException();
}
if (start < 0 || start >= src.length()) {
if (start < 0 || start > src.length()) {
throw new IndexOutOfBoundsException("Start " + start + " is outside of range [0;" + src.length() + ")");
}
if (end > src.length()) {

View File

@ -56,7 +56,7 @@ public abstract class TDoubleBuffer extends TBuffer implements Comparable<TDoubl
abstract void putElement(int index, double value);
public TDoubleBuffer get(double[] dst, int offset, int length) {
if (offset < 0 || offset >= dst.length) {
if (offset < 0 || offset > dst.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")");
}
if (offset + length > dst.length) {
@ -105,7 +105,7 @@ public abstract class TDoubleBuffer extends TBuffer implements Comparable<TDoubl
if (remaining() < length) {
throw new TBufferOverflowException();
}
if (offset < 0 || offset >= src.length) {
if (offset < 0 || offset > src.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + src.length + ")");
}
if (offset + length > src.length) {

View File

@ -56,7 +56,7 @@ public abstract class TFloatBuffer extends TBuffer implements Comparable<TFloatB
abstract void putElement(int index, float value);
public TFloatBuffer get(float[] dst, int offset, int length) {
if (offset < 0 || offset >= dst.length) {
if (offset < 0 || offset > dst.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")");
}
if (offset + length > dst.length) {
@ -105,7 +105,7 @@ public abstract class TFloatBuffer extends TBuffer implements Comparable<TFloatB
if (remaining() < length) {
throw new TBufferOverflowException();
}
if (offset < 0 || offset >= src.length) {
if (offset < 0 || offset > src.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + src.length + ")");
}
if (offset + length > src.length) {

View File

@ -56,7 +56,7 @@ public abstract class TIntBuffer extends TBuffer implements Comparable<TIntBuffe
abstract void putElement(int index, int value);
public TIntBuffer get(int[] dst, int offset, int length) {
if (offset < 0 || offset >= dst.length) {
if (offset < 0 || offset > dst.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")");
}
if (offset + length > dst.length) {
@ -105,7 +105,7 @@ public abstract class TIntBuffer extends TBuffer implements Comparable<TIntBuffe
if (remaining() < length) {
throw new TBufferOverflowException();
}
if (offset < 0 || offset >= src.length) {
if (offset < 0 || offset > src.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + src.length + ")");
}
if (offset + length > src.length) {

View File

@ -56,7 +56,7 @@ public abstract class TLongBuffer extends TBuffer implements Comparable<TLongBuf
abstract void putElement(int index, long value);
public TLongBuffer get(long[] dst, int offset, int length) {
if (offset < 0 || offset >= dst.length) {
if (offset < 0 || offset > dst.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")");
}
if (offset + length > dst.length) {
@ -105,7 +105,7 @@ public abstract class TLongBuffer extends TBuffer implements Comparable<TLongBuf
if (remaining() < length) {
throw new TBufferOverflowException();
}
if (offset < 0 || offset >= src.length) {
if (offset < 0 || offset > src.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + src.length + ")");
}
if (offset + length > src.length) {

View File

@ -56,7 +56,7 @@ public abstract class TShortBuffer extends TBuffer implements Comparable<TShortB
abstract void putElement(int index, short value);
public TShortBuffer get(short[] dst, int offset, int length) {
if (offset < 0 || offset >= dst.length) {
if (offset < 0 || offset > dst.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")");
}
if (offset + length > dst.length) {
@ -105,7 +105,7 @@ public abstract class TShortBuffer extends TBuffer implements Comparable<TShortB
if (remaining() < length) {
throw new TBufferOverflowException();
}
if (offset < 0 || offset >= src.length) {
if (offset < 0 || offset > src.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + src.length + ")");
}
if (offset + length > src.length) {

View File

@ -641,4 +641,11 @@ public class ByteBufferTest {
// expected
}
}
@Test
public void putGetEmptyArray() {
ByteBuffer bb = ByteBuffer.allocate(0);
bb.put(new byte[0]);
bb.get(new byte[0]);
}
}

View File

@ -432,4 +432,11 @@ public class CharBufferTest {
buffer.put("TeaVM");
assertThat(buffer.flip().toString(), is("TeaVM"));
}
@Test
public void putGetEmptyArray() {
CharBuffer cb = CharBuffer.allocate(0);
cb.put("");
cb.get(new char[0]);
}
}

View File

@ -372,4 +372,11 @@ public class DoubleBufferTest {
buffer.reset();
assertThat(buffer.position(), is(1));
}
@Test
public void putEmptyArray() {
DoubleBuffer db = DoubleBuffer.allocate(0);
db.put(new double[0]);
db.get(new double[0]);
}
}

View File

@ -372,4 +372,11 @@ public class FloatBufferTest {
buffer.reset();
assertThat(buffer.position(), is(1));
}
@Test
public void putEmptyArray() {
FloatBuffer fb = FloatBuffer.allocate(0);
fb.put(new float[0]);
fb.get(new float[0]);
}
}

View File

@ -386,4 +386,11 @@ public class IntBufferTest {
buffer.reset();
assertThat(buffer.position(), is(1));
}
@Test
public void putEmptyArray() {
IntBuffer ib = IntBuffer.allocate(0);
ib.put(new int[0]);
ib.get(new int[0]);
}
}

View File

@ -372,4 +372,11 @@ public class LongBufferTest {
buffer.reset();
assertThat(buffer.position(), is(1));
}
@Test
public void putEmptyArray() {
LongBuffer lb = LongBuffer.allocate(0);
lb.put(new long[0]);
lb.get(new long[0]);
}
}

View File

@ -372,4 +372,11 @@ public class ShortBufferTest {
buffer.reset();
assertThat(buffer.position(), is(1));
}
@Test
public void putEmptyArray() {
ShortBuffer sb = ShortBuffer.allocate(0);
sb.put(new short[0]);
sb.get(new short[0]);
}
}