From 81124a084b2d0b0842378a080f9e855dfdbaeba3 Mon Sep 17 00:00:00 2001 From: Kirill Prazdnikov Date: Wed, 21 Jun 2023 16:30:30 +0300 Subject: [PATCH] classlib: fix ByteBuffer,CharBuffer,*Buffer IOOB exception throw with zero length arrays in arguments fix #713 --- .../main/java/org/teavm/classlib/java/nio/TByteBuffer.java | 4 ++-- .../main/java/org/teavm/classlib/java/nio/TCharBuffer.java | 6 +++--- .../java/org/teavm/classlib/java/nio/TDoubleBuffer.java | 4 ++-- .../java/org/teavm/classlib/java/nio/TFloatBuffer.java | 4 ++-- .../main/java/org/teavm/classlib/java/nio/TIntBuffer.java | 4 ++-- .../main/java/org/teavm/classlib/java/nio/TLongBuffer.java | 4 ++-- .../java/org/teavm/classlib/java/nio/TShortBuffer.java | 4 ++-- .../java/org/teavm/classlib/java/nio/ByteBufferTest.java | 7 +++++++ .../java/org/teavm/classlib/java/nio/CharBufferTest.java | 7 +++++++ .../java/org/teavm/classlib/java/nio/DoubleBufferTest.java | 7 +++++++ .../java/org/teavm/classlib/java/nio/FloatBufferTest.java | 7 +++++++ .../java/org/teavm/classlib/java/nio/IntBufferTest.java | 7 +++++++ .../java/org/teavm/classlib/java/nio/LongBufferTest.java | 7 +++++++ .../java/org/teavm/classlib/java/nio/ShortBufferTest.java | 7 +++++++ 14 files changed, 64 insertions(+), 15 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/TByteBuffer.java b/classlib/src/main/java/org/teavm/classlib/java/nio/TByteBuffer.java index 8c89c25f7..561a2f51e 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/nio/TByteBuffer.java +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/TByteBuffer.java @@ -67,7 +67,7 @@ public abstract class TByteBuffer extends TBuffer implements TComparable= 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= 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) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/TCharBuffer.java b/classlib/src/main/java/org/teavm/classlib/java/nio/TCharBuffer.java index 122abf435..2f6a46a94 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/nio/TCharBuffer.java +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/TCharBuffer.java @@ -89,7 +89,7 @@ public abstract class TCharBuffer extends TBuffer implements Comparable= 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= 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= src.length()) { + if (start < 0 || start > src.length()) { throw new IndexOutOfBoundsException("Start " + start + " is outside of range [0;" + src.length() + ")"); } if (end > src.length()) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/TDoubleBuffer.java b/classlib/src/main/java/org/teavm/classlib/java/nio/TDoubleBuffer.java index 701bea316..6810117ff 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/nio/TDoubleBuffer.java +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/TDoubleBuffer.java @@ -56,7 +56,7 @@ public abstract class TDoubleBuffer extends TBuffer implements Comparable= 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= 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) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBuffer.java b/classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBuffer.java index 5e8a4fd4a..cad5e537e 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBuffer.java +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBuffer.java @@ -56,7 +56,7 @@ public abstract class TFloatBuffer extends TBuffer implements Comparable= 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= 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) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/TIntBuffer.java b/classlib/src/main/java/org/teavm/classlib/java/nio/TIntBuffer.java index f534218b3..572b5ca63 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/nio/TIntBuffer.java +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/TIntBuffer.java @@ -56,7 +56,7 @@ public abstract class TIntBuffer extends TBuffer implements Comparable= 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= 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) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/TLongBuffer.java b/classlib/src/main/java/org/teavm/classlib/java/nio/TLongBuffer.java index e2afead5e..6b5e13188 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/nio/TLongBuffer.java +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/TLongBuffer.java @@ -56,7 +56,7 @@ public abstract class TLongBuffer extends TBuffer implements Comparable= 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= 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) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/TShortBuffer.java b/classlib/src/main/java/org/teavm/classlib/java/nio/TShortBuffer.java index 3b443bda7..5e9362e85 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/nio/TShortBuffer.java +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/TShortBuffer.java @@ -56,7 +56,7 @@ public abstract class TShortBuffer extends TBuffer implements Comparable= 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= 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) { diff --git a/tests/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java b/tests/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java index 96cc08ff4..6135eebb5 100644 --- a/tests/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java @@ -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]); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/nio/CharBufferTest.java b/tests/src/test/java/org/teavm/classlib/java/nio/CharBufferTest.java index 86810ef12..f174bf5cd 100644 --- a/tests/src/test/java/org/teavm/classlib/java/nio/CharBufferTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/nio/CharBufferTest.java @@ -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]); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/nio/DoubleBufferTest.java b/tests/src/test/java/org/teavm/classlib/java/nio/DoubleBufferTest.java index db46a73bb..ea11e882a 100644 --- a/tests/src/test/java/org/teavm/classlib/java/nio/DoubleBufferTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/nio/DoubleBufferTest.java @@ -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]); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/nio/FloatBufferTest.java b/tests/src/test/java/org/teavm/classlib/java/nio/FloatBufferTest.java index 6dcc63ae7..5189866b5 100644 --- a/tests/src/test/java/org/teavm/classlib/java/nio/FloatBufferTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/nio/FloatBufferTest.java @@ -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]); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/nio/IntBufferTest.java b/tests/src/test/java/org/teavm/classlib/java/nio/IntBufferTest.java index b8e95c8b8..d7f207f99 100644 --- a/tests/src/test/java/org/teavm/classlib/java/nio/IntBufferTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/nio/IntBufferTest.java @@ -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]); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/nio/LongBufferTest.java b/tests/src/test/java/org/teavm/classlib/java/nio/LongBufferTest.java index c3dd72d09..ef1557d0a 100644 --- a/tests/src/test/java/org/teavm/classlib/java/nio/LongBufferTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/nio/LongBufferTest.java @@ -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]); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/nio/ShortBufferTest.java b/tests/src/test/java/org/teavm/classlib/java/nio/ShortBufferTest.java index 38e126bb8..bb506de62 100644 --- a/tests/src/test/java/org/teavm/classlib/java/nio/ShortBufferTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/nio/ShortBufferTest.java @@ -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]); + } }