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

The constructor StringBuffer(CharSequence) violates spec for negatively sized argument

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P3
    • Resolution: Approved
    • Fix Version/s: 13
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      behavioral
    • Compatibility Risk:
      minimal
    • Compatibility Risk Description:
      Hide
      Since JDK5, the exception thrown for negative lengths have been unspecified
      and inconsistent; with this change, NegativeArraySizeException will be thrown.
      Show
      Since JDK5, the exception thrown for negative lengths have been unspecified and inconsistent; with this change, NegativeArraySizeException will be thrown.
    • Scope:
      JDK

      Description

      Summary

      Javadoc for the constructor StringBuffer​(CharSequence seq) includes a promise that contradicts to the actual behavior.

      Problem

      Javadoc for the constructor StringBuffer​(CharSequence seq) states that "If the length of the specified CharSequence is less than or equal to zero, then an empty buffer of capacity 16 is returned." In fact, this promise is not kept with the current implementation: Passing negatively sized CharSequence results in an exception to be thrown.

      Moreover, the exception thrown is not consistent: For some inputs (if length is from range [-16, -1]) it is IndexOutOfBoundsException and for other inputs (if length is less then -16) it is NegativeArraySizeException. This behavior is also observed with the constructor StringBuilder(CharSequence).

      Solution

      It is proposed to remove the quoted sentence from the javadoc.

      For consistency, for any negatively sized argument, NegativeArraySizeException will be thrown from StringBuilder(CharSequence) and StringBuffer(CharSequence) constructors.

      Specification

      The javadoc will be modified as following:

      -     * <p>
      -     * If the length of the specified {@code CharSequence} is
      -     * less than or equal to zero, then an empty buffer of capacity
      -     * {@code 16} is returned.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                igerasim Ivan Gerasimov
                Reporter:
                igerasim Ivan Gerasimov
                Reviewed By:
                Roger Riggs
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: