https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108808

            Bug ID: 108808
           Summary: G++ -O2 incorrectly bypasses an infinite loop
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zhihan.yue at foxmail dot com
  Target Milestone: ---

Compile the following code with G++ -O2:

#include <string>
#include <iostream>

struct Test {
    bool a = false;
    std::string b;
};

int main() {
    Test test;
    test.a = false;
    std::cout << "enter" << std::endl;
    while(! test.a);
    std::cout << "passed" << std::endl;
    return 0;
}

Executing the program, the output will be:
enter
passed

This shows that the infinite loop was skipped.

Tested versions: (x64)
- Problem happens on GCC V10.1-10.4, V11.1-11.3, V12.1-12.2
- No such problem on GCC V9.5, V8.5, V7.5
Seems like a bug introduced in V10.1

Reply via email to