JDK-8235327 : the "while" loop output wrong integer
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 8,11.0.5
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: x86_64
  • Submitted: 2019-11-06
  • Updated: 2019-12-05
  • Resolved: 2019-12-05
Related Reports
Duplicate :  
Description
ADDITIONAL SYSTEM INFORMATION :
jdk version: jdk 1.8.231 and jdk 11.0.5 oracle official version.
os: mac and windows 10 x64.

A DESCRIPTION OF THE PROBLEM :
the int variable change  in the while loop, then output the result is wrong.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run the example code.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
always output "-1".
ACTUAL -
after a lot of loop, output the "/".

---------- BEGIN SOURCE ----------
public class WhileLoopTest {
  public void test1() {
    int i = 8;
    while ((i -= 3) > 0) ;
    System.out.println("i = " + i);

  }

  public static void main(String[] args) {
    WhileLoopTest tst = new WhileLoopTest();
    for (int i = 0; i < 50_000; i++) {
      tst.test1();
    }
  }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
just disable jit or use the AtomInteger.

  public void test2() {
    AtomicInteger i = new AtomicInteger(8);
    while ((i.addAndGet(-3)) > 0) ;
    System.out.println("i = " + i.intValue());
  }

FREQUENCY : always



Comments
it's dup of JDK-8231988. it has been fixed in both 11u and 8u.
04-12-2019

Already fixed in the higher version, Please use workaround.
04-12-2019

This issue is not observed in jdk14 ea b25. 14ea b25 - Pass 11.0.5 GA - Fail 8u231 - Fail
04-12-2019