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

(str) StringBuilder and StringBuffer fail when initialized with large strings

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • tbd
    • 7u45
    • core-libs

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      The initial capacity of a StringBuilder/Buffer when initialized with an existing String or CharSequence is the length of the original text + 16. If the original length is close to Integer.MAX_VALUE, the addition causes the sum to become negative and the allocation throws a NegativeArraySizeException.

      Consequently, Strings begin to malfunction before they reach their theoretical maximum length, because you can't append to them with `+` or `+=` any more.

      Original code in StringBuilder and StringBuffer:

          super(str.length() + 16);

        Suggested fix:

          super((int)Math.min(Integer.MAX_VALUE, str.length() + 16L));

      Likewise for the CharSequence constructors.


      REPRODUCIBILITY :
      This bug can be reproduced always.

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

              Created:
              Updated:
              Resolved: