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

Brian Burkhalter bpb at openjdk.org
Tue Jan 10 02:20:41 UTC 2023


On Mon, 9 Jan 2023 05:41:44 GMT, David Holmes <dholmes at openjdk.org> wrote:

>> Brian Burkhalter has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   8299684: Add test for out of range capacity passed to NewDirectBuffer
>
> make/test/JtregNativeJdk.gmk line 85:
> 
>> 83:   BUILD_JDK_JTREG_LIBRARIES_LIBS_libstringPlatformChars := -ljava
>> 84:   BUILD_JDK_JTREG_LIBRARIES_LIBS_libDirectIO := -ljava
>> 85:   BUILD_JDK_JTREG_LIBRARIES_LIBS_libNewDirectByteBuffer := -ljava -lc
> 
> Why do we need `-lc`?

No. Removed in d2386a4b73f0fa4eb9cc278b3cbb1578e6cb4b55.

> src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template line 245:
> 
>> 243:     // Constrain the capacity to int range throwing IAE if not possible
>> 244:     //
>> 245:     private static int clampCapacity(long capacity) {
> 
> I think `checkCapacity` would be better - clamp suggests the value gets clamped rather than throwing an exception.

So changed in d2386a4b73f0fa4eb9cc278b3cbb1578e6cb4b55.

> src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template line 247:
> 
>> 245:     private static int clampCapacity(long capacity) {
>> 246:         try {
>> 247:             return Math.toIntExact(capacity);
> 
> Why not the more direct and obvious:
> 
> if (capacity < 0) {
>   throw ...
> } else if (capacity > Integer.MAX_VALUE) {
>   throw ...
> }

So changed in d2386a4b73f0fa4eb9cc278b3cbb1578e6cb4b55.

> src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template line 251:
> 
>> 249:             String msg = "JNI NewDirectByteBuffer passed illegal capacity: "
>> 250:                 + capacity + (capacity < Integer.MIN_VALUE
>> 251:                 ? " < Integer.MIN_VALUE" : " > Integer.MAX_VALUE");
> 
> Why the check against `MIN_VALUE`?

It was the lower bound for `toIntExact()` but not relevant any more.

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

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


More information about the nio-dev mailing list