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

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

XMLWordPrintable

    • b11
    • Verified

      22The specification states:

      """
      If the length of the specified CharSequence is less than or equal to zero, then an empty buffer of capacity 16 is returned.uuuu
      """

      However, the current implementation throws NegativeArraySizeException instead.

      Here's the testcase to demonstrate the issue:

      public class U {
          public static void main(String[] args) {
              CharSequence seq = new MyNegLenCharSeq();
              StringBuffer sb = new StringBuffer(seq);
              System.out.println(sb.capacity());
          }

          private static class MyNegLenCharSeq implements CharSequence {
              public char charAt(int i) {
                  throw new UnsupportedOperationException();
              }
              public int length() { return -42; }
              public CharSequence subSequence(int st, int e) {
                  throw new UnsupportedOperationException();
              }
              public String toString() { return ""; }
          }
      }

      igerasim@ivirt16:~/work/8218227$ ~/java13/jdk/bin/java -showversion U
      java version "13-ea" 2019-09-17
      Java(TM) SE Runtime Environment (build 13-ea+6)
      Java HotSpot(TM) 64-Bit Server VM (build 13-ea+6, mixed mode, sharing)
      Exception in thread "main" java.lang.NegativeArraySizeException: -26
      at java.base/java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:86)
      at java.base/java.lang.StringBuffer.<init>(StringBuffer.java:139)
      at java.base/java.lang.StringBuffer.<init>(StringBuffer.java:169)
      at U.main(U.java:5)

            igerasim Ivan Gerasimov
            igerasim Ivan Gerasimov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: