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

(array) Incorrect exceptions in Arrays.copyOfRange

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 6
    • core-libs
    • Fix Understood
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]


      A DESCRIPTION OF THE PROBLEM :
      Please look at the source code of the new methods java.util.Arrays.copyOfRange (Java 1.6). They do not contain any checks of "from" argument. For example:
          public static byte[] copyOfRange(byte[] original, int from, int to) {
              int newLength = to - from;
              if (newLength < 0)
                  throw new IllegalArgumentException(from + " > " + to);
              byte[] copy = new byte[newLength];
              System.arraycopy(original, from, copy, 0,
                  Math.min(original.length - from, newLength));
              return copy;
          }
      Calling copyOfRange with very low "from" argument (for example, -1000000000) will lead to OutOfMemory instead of obvious exception alike "from argument is negative". Moreover, if from==Integer.MIN_VALUE, the result is very strange:
      "Exception in thread "main" java.lang.IllegalArgumentException: -2147483648 > NNN"
      (where NNN is the value of "to" argument).

      Please include the check of "from" argument into these methods.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the attached test.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Understandable IndexOutOfBounds exception.
      ACTUAL -
      Very strange exceptions: "java.lang.IllegalArgumentException: -2147483648 > 10"

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Arrays;
      public class CopyOfRangeTest {
          public static void main(String[] args) {
              int[] a = new int[] {0,1,2,3,4,5,6,7,8,9};
              int[] b = Arrays.copyOfRange(a, Integer.MIN_VALUE, 10);
              // If we'll use Integer.MIN_VALUE+100 instead Integer.MIN_VALUE,
              // OutOfMemoryError will be thrown
              for (int k = 0; k < b.length; k++)
                  System.out.print(b[k] + " ");
          }
      }

      ---------- END SOURCE ----------

            jrose John Rose
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: