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

(loom) Original impl of j.io.Writer::write(String, int, int) throws NegativeArraySizeException instead of the required IOOBE

    XMLWordPrintable

    Details

      Description

      === java.io.Writer::write(String, int, int) =====
      IndexOutOfBoundsException – Implementations should throw this exception if off is negative, or len is negative, or off + len is negative or greater than the length of the given string
      ========

      Code sample:

      import java.io.IOException;
      import java.io.Writer;

      public class Test {
          public static void main(String[] args) throws IOException {
              Writer writer = new Writer() {
                  @Override public void write(char[] cbuf, int off, int len) {}
                  @Override public void flush() {}
                  @Override public void close() {}
              };
              // len is negative :
              writer.write("0123456789", 2, -1);
          }
      }


      JDK-mainline:

      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Range [2, 1) out of bounds for length 10
      at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
      at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
      at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
      at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
      at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
      at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
      at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
      at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4586)
      at java.base/java.lang.String.getChars(String.java:1669)
      at java.base/java.io.Writer.write(Writer.java:289)
      at Test.main(Test.java:11)


      JDK-loom:

      Exception in thread "main" java.lang.NegativeArraySizeException: -1
      at java.base/java.io.Writer.write(Writer.java:274)
      at Test.main(Test.java:11)

        Attachments

          Activity

            People

            Assignee:
            alanb Alan Bateman
            Reporter:
            dbessono Dmitry Bessonov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: