8289365: SegmentAllocator:allocateArray(MemoryLayout, count) does not throw IAEx when count is -1

Reviewed-by: psandoz
This commit is contained in:
Maurizio Cimadamore 2022-07-12 14:22:42 +00:00
parent 0fd1b68972
commit 4545ed6842
2 changed files with 8 additions and 0 deletions
src/java.base/share/classes/java/lang/foreign
test/jdk/java/foreign

@ -319,6 +319,9 @@ public interface SegmentAllocator {
*/
default MemorySegment allocateArray(MemoryLayout elementLayout, long count) {
Objects.requireNonNull(elementLayout);
if (count < 0) {
throw new IllegalArgumentException("Negative array size");
}
return allocate(MemoryLayout.sequenceLayout(count, elementLayout));
}

@ -160,6 +160,11 @@ public class TestSegmentAllocators {
allocator.allocate(1, 3);
}
@Test(dataProvider = "allocators", expectedExceptions = IllegalArgumentException.class)
public void testBadAllocationArrayNegSize(SegmentAllocator allocator) {
allocator.allocateArray(ValueLayout.JAVA_BYTE, -1);
}
@Test(expectedExceptions = OutOfMemoryError.class)
public void testBadArenaNullReturn() {
SegmentAllocator segmentAllocator = SegmentAllocator.newNativeArena(MemorySession.openImplicit());