https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91215
Bug ID: 91215 Summary: Compiled program loops endlessly because of -O2 with g++ 8.3.0 Product: gcc Version: 8.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: sboisvert at gydle dot com Target Milestone: --- Created attachment 46614 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=46614&action=edit Minimum reproducer C++ source code Hello, I compile my program with -O2 with g++ 8.3.0. When I run my program, there is an infinite loop. With -O0, this is not the case. Steps to reproduce ================== Step 1: Acquire g++-8 (8.3.0) I used g++ (Ubuntu 8.3.0-6ubuntu1~18.04.1) 8.3.0. Step 2: build with O2 Command: /usr/bin/g++-8 -O2 g++-8.3.0-infinite-loop-bug.cpp -o g++-8.3.0-infinite-loop-bug Step 3: execute the program ./g++-8.3.0-infinite-loop-bug Expected result =============== The body of the for loop is done 50 times (i = 0, ..., i = 49). Then the program returns 0. Actual result ============= The body of the for loop is performed beyond i = 49. There is an infinite loop. Minimum reproducer ================== /* * Buggy for loop with g++ (Ubuntu 8.3.0-6ubuntu1~18.04.1) 8.3.0 * * /usr/bin/g++-8 -O2 g++-8.3.0-infinite-loop-bug.cpp -o g++-8.3.0-infinite-loop-bug ./g++-8.3.0-infinite-loop-bug */ #include <iostream> int doY() { for (int i = 0; i < 50; ++i) { std::cout << "DEBUG []"; std::cout << " i: " << i; std::cout << std::endl; } // return 0; // This fixes the bug. } int main(int argc, char* argv[]){ doY(); return 0; }