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

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

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 13
    • core-libs
    • None
    • behavioral
    • minimal
    • 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.
    • SE

      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.

            igerasim Ivan Gerasimov
            igerasim Ivan Gerasimov
            Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: