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

XMLWordPrintable

    • Type: Bug
    • Resolution: Duplicate
    • Priority: P4
    • tbd
    • Affects Version/s: 7u45
    • Component/s: 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.

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

              Created:
              Updated:
              Resolved: