Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8299759

(bf) JNI direct buffer functions with large capacity behave unexpectedly

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 21
    • core-libs
    • None
    • behavioral
    • minimal
    • Minimal risk as it seems unlikely that anything would depend on this aberrant behavior.
    • Java API
    • SE

      Summary

      Amend the specification of the JNI function NewDirectByteBuffer to specify that IllegalArgumentException is thrown when the value of the capacity parameter is negative or greater than Integer.MAX_VALUE.

      Problem

      The JNI function NewDirectByteBuffer is specified to have a jlong capacity parameter, but the corresponding java.net.ByteBuffer class only has an int capacity. The behavior of NewDirectByteBuffer is undefined if its long-valued capacity parameter is out of range for an int value. If the value of (int)capacity is negative, then an IllegalArgumentException is thrown. If however capacity != (int)capacity and (int)capacity > 0, then a direct buffer of an unexpected size will be allocated. This occurs for example if capacity equals 5000000000L in which case (int)capacity equals 705032704.

      Solution

      Change NewDirectByteBuffer to throw IllegalArgumentException if capacity < 0 or capacity > Integer.MAX_VALUE.

      Specification

      @@ -3668,20 +3668,24 @@
      
       `env`: the JNI interface pointer, must not be `NULL`.
      
       `address`: the starting address of the memory region, must not be `NULL`.
      
      -`capacity`: the size in bytes of the memory region, must be positive.
      +`capacity`: the size in bytes of the memory region, must be nonnegative and
      +less than or equal to `Integer.MAX_VALUE`
      
       #### RETURNS:
      
       Returns a local reference to the newly-instantiated `java.nio.ByteBuffer`
       object. Returns `NULL` if an exception occurs, or if JNI access to direct
       buffers is not supported by this virtual machine.
      
       #### THROWS:
      
      +`IllegalArgumentException`: if `capacity` is negative or greater than
      +`Integer.MAX_VALUE`
      +
       `OutOfMemoryError`: if allocation of the `ByteBuffer` object fails

            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Alan Bateman, David Holmes
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: