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

StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE

XMLWordPrintable

    • b94
    • 7
    • b118
    • generic
    • generic

      java.lang.StringBuilder.ensureCapacity(int minCap) method throws OutOfMemoryError Exception with the minCap value near to the Integer.MIN_VALUE.

      See the following simple code:

      public class TestEnsureCapacity {

          public static void main(String[] args) {
              StringBuilder sb = new StringBuffer("abc");
              int cap = sb.capacity();
              sb.ensureCapacity(Integer.MIN_VALUE);
          }

      }

      The Exception message will be thrown:

      Exception in thread "main" java.lang.OutOfMemoryError
              at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:125)
              at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:112)
              at java.lang.AbstractStringBuilder.ensureCapacity(AbstractStringBuilder.java:103)
              at java.lang.StringBuilder.ensureCapacity(StringBuilder.java:72)
              at TestEnsureCapacity.main(TestEnsureCapacity.java:6)

      In that time JDK specification say nothing about possible exceptions for the java.lang.StringBuilder.ensureCapacity method: If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.

      This is regression since JDK 7 b94.

            mchung Mandy Chung (Inactive)
            afomin Alexander Fomin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: