Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private static long allocateNativeInternal(long byteSize, long byteAlignment, Me
long allocationSize;
long allocationBase;
long result;
if (byteAlignment > MAX_MALLOC_ALIGN) {
if (byteAlignment > MAX_MALLOC_ALIGN || alignedSize % byteAlignment != 0) {
allocationSize = alignedSize + byteAlignment - MAX_MALLOC_ALIGN;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The calculation of allocationSize looks no longer correct now that byteAlignment might be smaller than MAX_MALLOC_ALIGN, and it can result in a negative size.

if (shouldReserve) {
AbstractMemorySegmentImpl.NIO_ACCESS.reserveMemory(allocationSize, byteSize);
Expand Down
8 changes: 7 additions & 1 deletion test/jdk/java/foreign/TestMemoryAlignment.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ public void testAlignedAccess(long align) {
assertEquals(aligned.byteAlignment(), align); //unreasonable alignment here, to make sure access throws
VarHandle vh = aligned.varHandle();
try (Arena arena = Arena.ofConfined()) {
MemorySegment segment = arena.allocate(aligned);;
MemorySegment segment = arena.allocate(aligned);
vh.set(segment, 0L, -42);

// Allocate another segment and fill it with data to
// check that the first segment is not overwritten
MemorySegment nextSegment = arena.allocate(aligned);
vh.set(nextSegment, 0L, 0xffffff);

int val = (int)vh.get(segment, 0L);
assertEquals(val, -42);
}
Expand Down