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

Strange optimisation in for loop with cyclic integer condition

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 6u6
    • hotspot
    • x86
    • linux

      FULL PRODUCT VERSION :
      java version "1.6.0_06"
      Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
      Java HotSpot(TM) Server VM (build 10.0-b22, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux 2.6.24-19-generic #1 SMP Wed Aug 20 22:56:21 UTC 2008 i686 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      A loop cycling through integers, passing through the Integer.MAX_VALUE and into negative numbers can result in the loop condition being interpreted falsely by the compiler. The loop should be infinite with the condition for ever being true.

      This situation only occurs when the Java -server option is used and is therefore probably caused by loop optimisations.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the code with the java -server option enabled.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Infinite loop
      ACTUAL -
      Loop breaks after about 15000 iterations.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Puzzles {
      public static void main(String[] args) throws InterruptedException {
      int END = Integer.MAX_VALUE;
      int count = 0;
      for(int i = Integer.MAX_VALUE - 5; i <= END; i++) {
      System.out.println(END + " -> " + i + " = " + (i <= END));
      count++;
      }
      System.out.println("broken " + count);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Either :

      - Use the java -client option instead of java -server.

      - Change the int END assignment to final int END

      - Use Integer.MAX_VALUE constant in the for loop instead of the END variable.

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: