RFR: 8299684: (bf) JNI direct buffer functions with large capacity behave unexpectedly [v5]

David Holmes dholmes at openjdk.org
Tue Jan 10 06:06:55 UTC 2023


On Tue, 10 Jan 2023 02:20:34 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> Remove cast in `JNI::NewDirectByteBuffer`of `long` capacity to `int`, modify the constructor in question to accept a `long` capacity, and verify in the constructor that the  capacity does not overflow `int` range, throwing IAE If it does.
>
> Brian Burkhalter has updated the pull request incrementally with one additional commit since the last revision:
> 
>   8299684: Changes addressing reviewer comments

Updates look good - thanks. A few minor nits.

src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template line 197:

> 195:             throw new IllegalArgumentException
> 196:                 ("JNI NewDirectByteBuffer passed capacity < 0: ("
> 197:                 + capacity + " < 0)");

You don't need to repeat the `< 0` part.

src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template line 201:

> 199:             throw new IllegalArgumentException
> 200:                 ("JNI NewDirectByteBuffer passed capacity > Integer.MAX_VALUE: ("
> 201:                 + capacity + " > Integer.MAX_VALUE)");

You don't need to repeat the `> Integer.MAX_VALUE` part.

test/jdk/java/nio/jni/NewDirectByteBuffer.java line 73:

> 71:     public static void main(String[] args) {
> 72:         System.out.println("--- Legal Capacities ---");
> 73:         for (long cap : LEGAL_CAPACITIES) {

I would still expect this to throw OOME for the very large values, so the OOME needs to be caught so that the test continues.

test/jdk/java/nio/jni/NewDirectByteBuffer.java line 106:

> 104:     private static native ByteBuffer newDirectByteBuffer(long size);
> 105:     private static native long getDirectBufferCapacity(ByteBuffer buf);
> 106:     private static native void freeDirectBufferMemory(ByteBuffer buf);

Naming nit: ByteBuffer versus Buffer

-------------

PR: https://git.openjdk.org/jdk/pull/11873


More information about the nio-dev mailing list