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

Provide public method to insure secure maximum array size

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 7
    • core-libs

      A DESCRIPTION OF THE REQUEST :
      This method eases the recalculation of insufficient array size.
      Some VMs reserve some header words in an array.
      Attempts to allocate larger arrays may result in OutOfMemoryError: Requested array size exceeds VM limit
      java.lang.System seems to be an appropriate home.


      JUSTIFICATION :
      Increasing an array is a common task in the JDK.
      Exemplary use case: see bug 7153238



      ---------- BEGIN SOURCE ----------
          private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

          /**
           * @param old the current size of the array
           * @param size the requested size (must not be bigger than the old size)
           */
          public int secureArraySize(int old, int size) {
              // overflow-conscious code
              assert (size <= old);
              if (size - MAX_ARRAY_SIZE <= 0)
                  return size;
              if (++old < 0) // overflow
                  throw new OutOfMemoryError
                          ("Required array size too large");
              return (old > MAX_ARRAY_SIZE) ?
                      Integer.MAX_VALUE : MAX_ARRAY_SIZE;
          }

      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: