Skip to content
Open
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 @@ -327,6 +327,11 @@ public static void copy(byte[] src, int srcOffset, ByteBuffer dest, int destOffs
destBase = dest.array();
}
long srcAddress = srcOffset + BYTE_ARRAY_BASE_OFFSET;
assert (src.length - srcOffset) >= length : "unsafe memory access: attempting to copy "
+ length + " bytes from src when only " + (src.length - srcOffset) + " capacity remains";
assert (dest.capacity() - destOffset) >= length : "unsafe memory access: attempting to copy "
+ length + " bytes into dest when only " + (dest.capacity() - destOffset)
+ " capacity remains.";
unsafeCopy(src, srcAddress, destBase, destAddress, length);
Copy link
Member Author

Choose a reason for hiding this comment

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

These assert are protecting calls to the method invoked by unsafeCopy: Unsafe.copyMemory. The documentation on that method makes no mention of bounds checking, and indeed I don't think it can because it accepts Object,long as parameters describing the source and destination memory addresses.

From AdoptOpenJDK:

    /**
     * Sets all bytes in a given block of memory to a copy of another
     * block.
     *
     * <p>This method determines each block's base address by means of two parameters,
     * and so it provides (in effect) a <em>double-register</em> addressing mode,
     * as discussed in {@link #getInt(Object,long)}.  When the object reference is null,
     * the offset supplies an absolute base address.
     *
     * <p>The transfers are in coherent (atomic) units of a size determined
     * by the address and length parameters.  If the effective addresses and
     * length are all even modulo 8, the transfer takes place in 'long' units.
     * If the effective addresses and length are (resp.) even modulo 4 or 2,
     * the transfer takes place in units of 'int' or 'short'.
     *
     * @since 1.7
     */
    public native void copyMemory(Object srcBase, long srcOffset,
                                  Object destBase, long destOffset,
                                  long bytes);

}

Expand Down Expand Up @@ -361,6 +366,10 @@ public static void copy(ByteBuffer src, int srcOffset, byte[] dest, int destOffs
srcBase = src.array();
}
long destAddress = destOffset + BYTE_ARRAY_BASE_OFFSET;
assert (src.capacity() - srcOffset) >= length : "unsafe memory access: attempting to copy "
+ length + " bytes from src when only " + (src.capacity() - srcOffset) + " capacity remains";
assert (dest.length - destOffset) >= length : "unsafe memory access: attempting to copy "
+ length + " bytes into dest when only " + (dest.length - destOffset) + " capacity remains.";
unsafeCopy(srcBase, srcAddress, dest, destAddress, length);
}

Expand Down Expand Up @@ -390,6 +399,11 @@ public static void copy(ByteBuffer src, int srcOffset, ByteBuffer dest, int dest
destAddress = destOffset + BYTE_ARRAY_BASE_OFFSET + dest.arrayOffset();
destBase = dest.array();
}
assert (src.capacity() - srcOffset) >= length : "unsafe memory access: attempting to copy "
+ length + " bytes from src when only " + (src.capacity() - srcOffset) + " capacity remains";
assert (dest.capacity() - destOffset) >= length : "unsafe memory access: attempting to copy "
+ length + " bytes into dest when only " + (dest.capacity() - destOffset)
+ " capacity remains.";
unsafeCopy(srcBase, srcAddress, destBase, destAddress, length);
}

Expand Down