Skip to content

Commit

Permalink
[Java] Call bound check instead of capacity expansion in AbstractMuta…
Browse files Browse the repository at this point in the history
…bleDirectBuffer.getInt (#285)
  • Loading branch information
fbaro authored Aug 30, 2023
1 parent ba5935b commit a2b406f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ public void putLong(final int index, final long value)
*/
public int getInt(final int index, final ByteOrder byteOrder)
{
ensureCapacity(index, SIZE_OF_INT);
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_INT);
}

int bits = UNSAFE.getInt(byteArray, addressOffset + index);
if (NATIVE_BYTE_ORDER != byteOrder)
Expand Down
16 changes: 16 additions & 0 deletions agrona/src/test/java/org/agrona/ExpandableArrayBufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.agrona;

import java.nio.ByteOrder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
Expand Down Expand Up @@ -144,4 +145,19 @@ void checkLimitIncreaseBufferCapacity(final int limit)
assertTrue(limit <= buffer.capacity());
assertNotSame(originalArray, buffer.byteArray());
}

@Test
void shouldThrowExceptionForReadingIntAboveCapacity()
{
final MutableDirectBuffer buffer = newBuffer(3);
assertThrows(IndexOutOfBoundsException.class, () -> buffer.getInt(4, ByteOrder.BIG_ENDIAN));
}

@Test
void shouldThrowExceptionForReadingLongAboveCapacity()
{
final MutableDirectBuffer buffer = newBuffer(3);
assertThrows(IndexOutOfBoundsException.class, () -> buffer.getLong(4, ByteOrder.BIG_ENDIAN));
}

}

0 comments on commit a2b406f

Please sign in to comment.