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

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

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 7u45
    • Fix Version/s: tbd_major
    • Component/s: core-libs
    • Labels:

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: